Why is String.IsNullOrEmpty faster than String.Length?

前端 未结 7 2066
心在旅途
心在旅途 2020-12-14 17:28

ILSpy shows that String.IsNullOrEmpty is implemented in terms of String.Length. But then why is String.IsNullOrEmpty(s) faster than

7条回答
  •  旧时难觅i
    2020-12-14 17:59

    I think it is impossible IsNullOrEmpty to be faster because as all the rest said it also makes a check for null. But faster or not the difference is going to be so small, that this gives a plus on using IsNullOrEmpty just because of this additional null check that makes your code safer.

提交回复
热议问题