开发者

why string.IsNullOrEmpty() is created?

If string.Empty != null why string.IsNullOrEmpty() is created?

I just want to sa开发者_如何学JAVAy that:

if null and string.Empty are different to each other.

  • why string.IsNull(); and string.IsEmpty(); separate methods does not exist.
  • why a combined method string.IsNullOrEmpty() exists?


  • string.IsNull doesn't exist because you'd just check for the reference being null
  • string.IsEmpty doesn't exist because you can easily compare for equality with "" or for a length of 0
  • string.IsNullOrEmpty exists because it's simpler to write the single method call than use

       if (text == null || text.Length == 0)
    

    (or the inverse, of course).

Each of the individual checks can be done simply on its own, but it's convenient to have a combination of the two.


It's for checking that the input string is a valid one. (e.g, not null and not empty). So you don't want to do both the checks each time you want to ensure that so that's why it is made for. If you want to check either of the single ones you can just use the == null or == "" compares.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜