C# Empty Strings

String type

An empty string has zero characters. These strings can be checked using a clear and efficient method. Developers frequently need to see if a string contains no characters. Microsoft's FxCop has one recommendation.

Example

Length property

Here we see the five empty string tests shown in the table above. Each of the five tests will be true, as the string "str" is empty. The code prints out the results of the tests, which you can see at the end.

Result:Strings were compared against empty strings. Checking Length was the fastest.

String Length
C# program that uses empty strings

class Program
{
    static void Main()
    {
	// Example string
	string str = "";

	// Example tests
	if (str == "")
	{
	    // 1
	    // Test with op_Equality
	    System.Console.WriteLine(1);
	}
	if (string.Equals(str, ""))
	{
	    // 2
	    // Test with static Equals method
	    System.Console.WriteLine(2);
	}
	if (string.IsNullOrEmpty(str))
	{
	    // 3
	    // Test with IsNullOrEmpty
	    System.Console.WriteLine(3);
	}
	if (str.Length == 0)
	{
	    // 4
	    // Test with Length property
	    System.Console.WriteLine(4);
	}
	if (str.Equals(""))
	{
	    // 5
	    // Test with Equals method
	    System.Console.WriteLine(5);
	}
    }
}

Output

1
2
3
4
5

Benchmark results

Equality operator ==:  796 ms
string.Equals:         811 ms
string.IsNullOrEmpty:  312 ms
Length:                140 ms [fastest]
Instance Equals:      1077 ms
Note

First example. This code shows the equality operator. For many developers, this is the clearest code, but it is not the fastest. But it won't cause severe performance problems. If you look at == in MSIL, it is called op_Equality.

Second example. This part shows the static Equals method. This has the same semantics as part 1 but uses different syntax. I haven't seen this method used as often. It is an alternate syntax form.

Framework: NET

Third example. IsNullOrEmpty is a newer static method that wasn't present in the first releases of the .NET Framework. It performs well and I have seen it often in the wild. More details are available on IsNullOrEmpty.

string.IsNullOrEmptyNull keyword

Fourth example. This shows the simple Length check.
FxCop,
Microsoft's static analysis tool,
recommends this method. However, it throws a NullReferenceException when the string is null. This condition occurs often in programs.

Null StringsNullReferenceException

Fifth example. This part demonstrates the instance Equals method. This calling pattern is different because the compiler must know the string "s" is not null before calling the method. This changes the performance.

Benchmark

Performance optimization

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, although I didn't measure any improvement in it.

The fastest way is to access the Length property. 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, improving this is worthwhile.

Note:Microsoft's FxCop warns when you use the other methods. It is right when it states that Length is the fastest method.

FxCop

String.Empty

Tip

In the string class, there is a public readonly field called Empty. You can access this field by typing string.Empty, but this field has some negatives and I prefer the constant itself. There is more material about string.Empty.

string.Empty

Summary

An empty string can be checked in many ways. We choose which one depending on program context and preferences. We saw five different ways. Observing benchmarks of this simple operation is important and useful.


C#: String