C#: String

.NET Array Collections File String Async Cast DataTable DateTime Dictionary Enum Exception For Foreach IEnumerable If IndexOf Lambda LINQ List Parse Path Process Property Regex Replace Sort Split StringBuilder Substring Switch Tuple


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.



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
	if (string.Equals(str, ""))
	    // 2
	    // Test with static Equals method
	if (string.IsNullOrEmpty(str))
	    // 3
	    // Test with IsNullOrEmpty
	if (str.Length == 0)
	    // 4
	    // Test with Length property
	if (str.Equals(""))
	    // 5
	    // Test with Equals method



Benchmark results

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

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.


Fourth example. This shows the simple Length check.
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.


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.




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.



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.