.NET 4 String Function to Check for Empty Strings

Question is...
How to Check an Empty Strings or not in ASP.NET 4.0?

How many times have you written code similar to this?

string firstName = FirstName.Text.Trim();

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

// or

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

Leave a Reply