String.IsNullOrEmpty

0 comments suggest edit

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.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

13 responses

  1. Avatar for Simone Chiaretta
    Simone Chiaretta August 23rd, 2006

    I agree!!!
    One of the best addition to .NET 2.0 :-)

  2. Avatar for Joshua Flanagan
    Joshua Flanagan August 23rd, 2006

    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)){}

  3. Avatar for haacked
    haacked August 23rd, 2006

    @Josh, I was going to write about that next, but your comment does the job for me. Thanks!

  4. Avatar for Willie Tilton
    Willie Tilton August 23rd, 2006

    What about the ?? operator?
    http://msdn2.microsoft.com/...
    Also, I had heard that there was a bug in this method. Has that been fixed?

  5. Avatar for haacked
    haacked August 23rd, 2006
  6. Avatar for Adam Vandenberg
    Adam Vandenberg August 23rd, 2006
  7. Avatar for Jon Galloway
    Jon Galloway August 23rd, 2006

    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.

  8. Avatar for haacked
    haacked August 23rd, 2006

    Umm that doesn't make sense. Because if myString was null, how could you call a method on it?

  9. Avatar for Jon Galloway
    Jon Galloway August 23rd, 2006

    Cheeky monkey.

  10. Avatar for Steve Campbell
    Steve Campbell August 24th, 2006

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

  11. Avatar for Dustin Campbell
    Dustin Campbell October 24th, 2006

    You can achieve an IsNullOrEmpty instance method by using an extension method. See my blog entry for details.

  12. Avatar for Rachna
    Rachna March 7th, 2011

    Thanks, that was very helpful.
    I have made vb.net version from this.

    Public Shared Function IsNullOrEmpty(ByVal value As String) As Object
    If value <> Nothing Then
    IsNullOrEmpty = (value.Length == 0)
    Else
    IsNullOrEmpty = True
    End If
    End Function

  13. Avatar for Sang
    Sang August 16th, 2012

    @Jon Galloway Aug 23, 2006 4:30 PM # 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()
    Its, string.IsNullorEmplty(myString)
    try this!!!