简体   繁体   中英

why string.IsNullOrEmpty() is created?

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

I just want to say 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. (eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM