String.IsNullOrEmpty

This post is an ode to one of my favorite, albeit extremely minor, additions to .NET 2.0.  This is the method that I am sure we have all written in some sort of StringHelper library of some sort, but are now glad it is included in the framework as it makes our code a tad bit cleaner and shuts up that pesky FxCop warning about using the length of the string to test for empty strings.

And under the hood, it does the right thing.

public static bool IsNullOrEmpty(string value)
{
      if (value != null)
      {
            return (value.Length == 0);
      }
      return true;
}

If you haven’t met this method, do get well acquainted.

What others have said

Requesting Gravatar... Simone Chiaretta Aug 23, 2006 12:32 PM
# Re: String.IsNullOrEmpty
I agree!!!
One of the best addition to .NET 2.0 :-)
Requesting Gravatar... Joshua Flanagan Aug 23, 2006 2:31 PM
# re: String.IsNullOrEmpty
Another nicety in the same vein is the new overload of String.Equals.

In pre-2.0, the recommended way to compare strings in a case-insensitive way was:

if ((String.Compare(a, b, true) == 0){}

(as performing a ToUpper() on both strings will not give an accurate comparison in all cultures)

Of course that syntax is just as clunky as comparing the length to 0.

Now, we just add an extra parameter to Equals():
if (a.Equals(b,StringComparison.CurrentCultureIgnoreCase)){}
Requesting Gravatar... haacked Aug 23, 2006 2:37 PM
# re: String.IsNullOrEmpty
@Josh, I was going to write about that next, but your comment does the job for me. Thanks!
Requesting Gravatar... Willie Tilton Aug 23, 2006 2:49 PM
# re: String.IsNullOrEmpty
What about the ?? operator?

http://msdn2.microsoft.com/en-us/library/ms173224.aspx

Also, I had heard that there was a bug in this method. Has that been fixed?
Requesting Gravatar... haacked Aug 23, 2006 2:57 PM
# re: String.IsNullOrEmpty
Already wrote about that.
Requesting Gravatar... Adam Vandenberg Aug 23, 2006 3:07 PM
# re: String.IsNullOrEmpty
But don't get TOO well acquainted?

Null Exemption caused by JIt optimisation around String.IsNullOrEmpty.
Requesting Gravatar... Jon Galloway Aug 24, 2006 12:30 AM
# re: String.IsNullOrEmpty
I like the static method, but I wish there were a member method (which just called the static) as well. I'm always thrown when myString.IsNullOrEmpty() isn't defined...

Fortunantly, when I'm thrown, it's just a simple "throw;" rather than "throw ex", which would wipe my stack trace and really confuse me.
Requesting Gravatar... haacked Aug 24, 2006 2:31 AM
# re: String.IsNullOrEmpty
Umm that doesn't make sense. Because if myString was null, how could you call a method on it?
Requesting Gravatar... Jon Galloway Aug 24, 2006 3:24 AM
# re: String.IsNullOrEmpty
Cheeky monkey.
Requesting Gravatar... Steve Campbell Aug 24, 2006 2:01 PM
# re: String.IsNullOrEmpty
Of course, in .NET 1.1, VB.NET allows the equal comparison to work with nulls anyway. So 'if myString="" then DoSomething' works if myString is null/nothing or if it is an empty string (see http://dukeytoo.blogspot.com/2005/12/things-i-learned-today.html).

Its not like there is never any valid business reason to differentiate between NULL strings and empty strings anyway. With numbers, there is the "did he enter 0 or nothing" argument, but with strings there is simply no reason to distinguish.
Requesting Gravatar... Dustin Campbell Oct 24, 2006 11:45 AM
# re: String.IsNullOrEmpty
You can achieve an IsNullOrEmpty instance method by using an extension method. See my blog entry for details.

What do you have to say?

(will show your gravatar)
Please add 2 and 6 and type the answer here: