.NET 2.0 and Empty Strings

I'm not a maniac of extreme performance testing (expecially when writing simple code) but there are some personal rules learned during the years that I always respect.

One of these rules was the way on how to test for empty strings with .NET. I've always learned that, despite all the simple methods provided by the .NET Framework, the most efficient and performant way to test for an empty string is to write something like

If MyStr.Length = 0 Then

(this code produce less MSIL instructions) but (as you know) new Framework = new instructions :P and today, after years and years of doing always the same test method, here that a new instruction appears me: String.IsNullOrEmpty. Wow... a new method? And with a so nice and intuitive name? This could be the ideal method for me...

My love for this method was increased after reading the official documentation and in particular this step:

You should be aware that Equals and Length == 0 behave differently for null strings. If you try to get the value of the Length property on a null string, the common language runtime throws a System.NullReferenceException. If you perform a comparison between a null string and the empty string, the common language runtime does not throw an exception; the comparison returns false. Testing for null does not significantly affect the relative performance of these two approaches. When targeting .NET Framework 2.0, use the IsNullOrEmpty(System.String) method. Otherwise, use the Length == comparison whenever possible.

Really interesting but... this method will be really performant?

I've decided to dress me up as a Francesco Balena and I've written a little VB.NET code with my Visual Studo 2005 to test the performances of the four methods available to test for an empty string.

The code is shown below and it's only a big loop that checks for an empty string and reports the elapsed time to do all the tests (the iteration number was choosen to have a significative final time):

Dim StartTime As DateTime = DateTime.Now
Dim i As
Integer
Dim MyStr As String =
""
For i = 0 To 1000000000
  'If MyStr = "" Then
  'If MyStr = String.Empty Then
  'If MyStr.Length = 0 Then
  If String.IsNullOrEmpty(MyStr) Then

  End If
Next
Dim TimeElapsed As Double = (DateTime.Now - StartTime).TotalMilliseconds
Console.WriteLine("Time elapsed:" + TimeElapsed.ToString)

I've launched the previous code 4 different times with the 4 different methods (by uncommenting the appropriate line) and these are the results (in milliseconds):

1) If MyStr="" Then

2) If MyStr= String.Empty Then

 

3) If MyStr.Length = 0 Then

4) If String.IsNullOrEmpty(MyStr) Then

Surprise or not? The "old" method MyStr.Length = 0 is really more fast and it's absolutely the best method to test for an empty string. However,  using If String.IsNullOrEmpty(MyStr) is a good choice on cases where you can obtain also a null value (but it's about 3 times slow).

Conclusion? I think that I'll continue to use my old method of checking the length of the string, except for particular cases.

P.S. Please NEVER USE If MyStr= String.Empty Then to test for an empty value... the better choice could be to remove it from the native Framework!

Print | posted on Wednesday, December 21, 2005 11:00 PM

Comments on this post

# re: .NET 2.0 and Empty Strings

Requesting Gravatar...
Humm... interesting,

So Although ISNullOrEmpty looks good but it takes more time,And it is better to takethe lenght of some thing

Keep up the good work
Left by software developers on Aug 17, 2009 6:31 PM

# re: .NET 2.0 and Empty Strings

Requesting Gravatar...
"Time elapsed:" + TimeElapsed.ToString)

I've launched the previous code 4 different times with the 4 different methods (by uncommenting the appropriate line) and these are the results (in milliseconds):

1) If MyStr="" Then
Left by wholesale laptop adapter on May 23, 2010 2:33 PM

# clothing manufacturer

Requesting Gravatar...
A more simple approach even for Windows SharePoint Services: The SharePoint Business Data List
Left by clothing manufacturer on Mar 06, 2011 3:40 PM

# re: .NET 2.0 and Empty Strings

Requesting Gravatar...
What about Len(MyStr) > 0? MyStr.Length will blow up if MyStr is Nothing, and Len() automatically handles Nothing. To fairly compare MyStr.Length to String.IsNullorEmpty, your comparison needs to be "If MyStr IsNot Nothing AndAlso MyStr.Length = 0". The same check for nothing needs to be done for the "=" commands too.
Left by developer on Sep 07, 2011 7:33 PM

# re: .NET 2.0 and Empty Strings

Requesting Gravatar...
Good work and good comments tooo..
Left by Sunil kumar on Feb 03, 2012 11:35 AM

Your comment:

 (will show your gravatar)
 
Please add 6 and 6 and type the answer here: