Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why string.IsNullOrEmpty() is created?

Tags:

string

c#

.net

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?
like image 977
Javed Akram Avatar asked Jun 26 '11 08:06

Javed Akram


1 Answers

  • 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.

like image 160
Jon Skeet Avatar answered Sep 19 '22 10:09

Jon Skeet