Why is string.IsNullOrEmpty () created?

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

I just want to say that:
if null and string.Empty are different from each other.

  • why string.IsNull(); and string.IsEmpty(); separate methods do not exist.
  • Why is there a combined string.IsNullOrEmpty() method?
+7
source share
2 answers
  • string.IsNull does not exist because you are just checking that the link is null
  • string.IsEmpty does not exist because you can easily compare for equality with "" or for length 0
  • string.IsNullOrEmpty exists because it is easier to write a call to one method than to use

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

    (or vice versa, of course).

Each of the individual checks can be performed simply by itself, but it is convenient to have a combination of the two.

+16
source

This is to validate the input string. (for example, not empty, not empty). That way, you don’t want to do both checks every time you want it to be what it is done for. If you want to check one of them, you can simply compare == null or == "" .

+1
source

All Articles