Music is the space between the notes.
Claude Debussy

ARCHITECTURE, n: The art of how to waste space.
Philip Johnson

Many times when dealing with data analysis, the following three conditions are identical.

  • null string
  • empty string
  • string containing only white space.

Prior to .NET 4 Framework, I could do something like the following code.  Please note, I have NOT figured out how to enter double ampersand to my WordPress blog so this code is even more verbose than it has to be.

public void SomeMethod(string value)
{
  // One Way
  if (value != null)
  {
    if (value.Trim() != string.Empty)
      DoSomething(value);
  }

  // Another Way
  if (!string.IsNullOrEmpty(value))
  {
    if (value.Trim() != string.Empty)
      DoSomething(value);
  }
}

Now, the code is very simple.  Much more elegant.

public void SomeMethod(string value)
{
  if (!string.IsNullOrWhiteSpace(value))
    DoSomething(value);
}

The image below shows the IntelliSense for IsNullOrWhiteSpace

Tags: ,