Dot Net Perls
C#

Empty String Use

by Sam Allen

Problem

Check for an empty string using the most efficient method possible. Often when developing, we need to see if a string contains no characters. (Here we know that the string is not null.) Microsoft's FxCop has one recommendation, but other developers have different preferences. We want the best way.

Solution: C#

There are several ways to test for an empty string, and some are more popular than others. As I note in my FxCop article, Microsoft recommends checking the length of the empty string. I explored IsNullOrEmpty in another article.

Methods to check empty strings

Here are the methods for checking for an empty string. After the example, I will describe my benchmark and present my results. Because this is so common, knowing the best way to do it is quite helpful. Let's look at the methods.

{
    int dummy = 0; // For example.
    string testStr = "cat";
    string testEmp = "";

    // 1. Test against an empty string.
    if (testEmp == "")
    {
        dummy++;
    }
    if (testStr != "")
    {
        dummy--;
    }

    // 2. Test with the static Equals method.
    if (string.Equals(testEmp, ""))
    {
        dummy++;
    }
    if (string.Equals(testStr, "") == false)
    {
        dummy--;
    }

    // 3. Test with IsNullOrEmpty.
    if (string.IsNullOrEmpty(testEmp))
    {
        dummy++;
    }
    if (string.IsNullOrEmpty(testStr) == false)
    {
        dummy--;
    }

    // 4. Test Length property.
    if (testEmp.Length == 0) // Highlighted because these two are the fastest.
    {
        dummy++;
    }
    if (testStr.Length != 0)
    {
        dummy--;
    }

    // 5. Test against empty string with instance method.
    if (testEmp.Equals(""))
    {
        dummy++;
    }
    if (testStr.Equals("") == false)
    {
        dummy--;
    }
}

Benchmark details

I ran these if statements through 100 million iterations each. I did this in an effort to improve an ASP.NET program that had to use these tests frequently, and although I didn't measure any improvement in it, I found the fastest and cleanest method.

Number Description Time in ms
1 == operator 796
2 string.Equals 811
3 IsNullOrEmpty 312
4 Length 140
5 instance Equals 1077

Benchmark results

Clearly, checking the length of the string is the fastest method of those presented here. If you are using any method other than IsNullOrEmpty, it is worth your time to change to writing code with Length checks. Microsoft's FxCop warns when you use the other methods, and it is right when it states that Length is the fastest method.

Conclusion

Access the Length property of a string to see if it is empty. If you need to detect null, consider IsNullOrEmpty. If your program uses a slow method to do this, and it runs thousands of times very frequently (such as on an ASP.NET page), improving this tiny part of your code is worthwhile. Performance is critical when developing, and here we can improve performance many times over.

© 2008 Sam Allen. All rights reserved.

Ads by The Lounge