.NET 4: IsNullOrEmpty vs IsNullOrWhiteSpace

You might be familiar with the following lines of code and may have used it often in your code when validating string values for null, empty or whitespace:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;

Now, .NET framework 4.0 provides a new API, IsNullOrWhiteSpace which does the same in a much neater way

return String.IsNullOrWhiteSpace(value);

It may not look like a big change, but makes code much more readable, especially if you have multiple string values to check in one if statement.

Advertisements

3 thoughts on “.NET 4: IsNullOrEmpty vs IsNullOrWhiteSpace

  1. Pingback: Kartik sehgal | Charlestonslip

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s