
An empty string has zero characters. You want to check for empty strings using a clear and efficient method. Developers frequently need to see if a string contains no characters. Microsoft's FxCop has one recommendation, while developers have many different preferences. We check for empty strings in the C# language and compare performance.
This C# article compares the performance of testing for empty strings.
Empty string testing
Strings were compared against empty strings.
Checking Length was the fastest.
Equality operator ==: 796 ms
string.Equals: 811 ms
string.IsNullOrEmpty: 312 ms
Length: 140 ms [fastest]
Instance Equals: 1077 msHere 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.
Program that uses empty strings [C#]
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
Here we look at the methods in more detail. 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.
First example. This code shows the equality operator. For many developers, this is the clearest code, but it is not the fastest. However, 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 1 but uses different syntax. I haven't seen this method used as often.
Third example. IsNullOrEmpty is a newer static method that wasn't present in the first releases of .NET. It performs well and I have seen it often in the wild.
string.IsNullOrEmpty MethodFourth 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. Null strings occur often in programs.
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 somewhat.

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. Please see the figures at the top of this article.
Performance summary. The fastest way is to access the Length property of a string. 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 tiny part of your code is worthwhile.
FxCop warning. Microsoft's FxCop warns when you use the other methods, and it is right when it states that Length is the fastest method. In my experiment, this is the case.
FxCop Performance Warnings
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 on this site.
string.Empty Example
Here we saw five different ways of checking for empty strings in the C# programming language. Which one you use likely depends on your team's guidelines or your personal preference, but observing the benchmarks is important and useful.
String Type