Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Tip: New .NET 4 String Function to Check for Empty Strings

Pete Brown - 09 February 2010

How many times have you written code similar to this?

string firstName = FirstName.Text.Trim();

if (!string.IsNullOrEmpty(firstName))
    // do something


if (someParam == null || string.IsNullOrEmpty(someParam.Trim())
    throw new ArgumentException("Empty string", "someParam");

// do something

A nice little time-saver for those guard functions is now included in .NET 4: the IsNullOrWhiteSpace function:

if (!string.IsNullOrWhiteSpace(FirstName.Text))
    // do something

IsNullOrWhiteSpace checks for null, empty or whitespace characters.

Sure, it’s a little thing, but it’s nice to have it in there, especially considering we get all sorts of nice new little things, and the client framework install is still smaller than it was in .NET 3.5

posted by Pete Brown on Tuesday, February 9, 2010
filed under:      

18 comments for “Tip: New .NET 4 String Function to Check for Empty Strings”

  1. TAGsays:
    TrimStart() can avoid allocation.

    There is no reasons to do trim from both sides - as if string is empty - then it will be empty even then trimming from start.

    But! This practice is flawed. In case if you are trimming strings for purpose of validation - then you should deal with trimmed string for actual processing.

    So you should check for null or empty, then trim and save, then check again, then process data using trimmed data.
  2. Bob Valesays:
    <p>But an extension method doesn't care if it is null because the compiler rewrites it from </p><p>FirstName.Text.IsNullOrWhiteSpace() to string.IsNullOrWhiteSpace(FirstName.Text) you can just use the easier to read form.</p>
  3. Jacob O'Reillysays:
    Since it would be an extension method, it wouldn't be a problem that the string being used was null. Under the covers it just calls the method with the string as its parameter.
  4. Khalossays:
    If they are were extension methods like Oskar suggests, it doesn't matter if the instance is null. Extension methods are just syntactic sugar.

    s.ExtensionMethod() is the same as ExtensionClass.ExtensionMethod(s) even is s is null.

    That's one of the great uses of extension methods, which I think is what Oskar was getting it, they didn't use extension methods, so we can't do this.
  5. Erik van Brakelsays:
    @Pete.Brown: It would still work, but I would agree if you said it would be odd. This would work:

    public static bool IsNullOrWhiteSpace(this input)
    return string.IsNullOrWhiteSpace(input);

    As long as you're not calling any methods/properties on the input string it won't fail if it's null. Looks odd though.
  6. Pete Brownsays:
    True all.

    I should have stated that it looks really wrong to me when the value is null. I'll get over it, though :)

    As for extension methods in general, as pointed out, you can do that yourself if you want. I think the problem with extension methods in the framework is that it isn't always discoverable. Adding in a namespace (if the method is in System no problems) but adding in a namespace and suddenly getting new functionality on existing types tends to screw new folks up. Linq is a good example of this.

    I like extension methods for their utility, but I think they have discoverability issues.

  7. Josh Einsteinsays:
    I hate the idea of it being an extension method because as noted, it feels totally wrong to have some methods (extension methods) work on null references and others not.

    To Joe up top, yes it avoids the allocation by checking each character with Char.IsWhiteSpace. It doesn't do Trim().Length internally.
  8. Richardsays:
    <p>Of course, it's trivial to write your own version of this function in v3.5: </p><p>public static bool IsNullOrWhiteSpace(this string value) </p><p>{ </p><p>  if (string.IsNullOrEmpty(value)) </p><p>    return true; </p><p>  return value.All(char.IsWhiteSpace); </p><p>} </p>
  9. Matthew Sposatosays:
    For the string.IsNullOrWhiteSpace test, does whitespace include tabs and newline characters? I would test this myself, but here at work we're still using .NET 3.5 / VS2008. I'll play with this when I get home though.

Comment on this Post

Remember me

1 trackback for “Tip: New .NET 4 String Function to Check for Empty Strings”

  1. uberVU - social commentssays:
    This post was mentioned on Twitter by ash2mud: It's the little things, new in .NET 4: string.IsNullOrWhiteSpace() http://bit.ly/b5Bhp1