C# Empty String ExamplesUse empty strings and the string.Empty field. Test the performance of empty strings.
The string.Empty field is an empty string literal. It is not the same as an empty string literal constant "". There is a subtle difference.Strings
We compare ways
to create, and test, empty strings. An understanding of string literals is helpful here. There are some minor performance differences (benchmarks show these).String Literal
A first example.
Here we use string.Empty and "" in a C# program. The string.Empty field is initialized to "" at runtime by the .NET Framework.
And: This means string.Empty cannot be used in a switch. But we can test with string.Empty in if-statements.If
Assign: We assign to string.Empty. This is mostly equivalent to "". We test the variable against the constant "", which returns true.
Finally: The program tests the Length and switches on the value. We see the usage of string.Empty.
C# program that uses string.Empty
static void Main()
// Initialize string to empty string field.
string value = string.Empty;
// Test the empty field.
if (value == "")
if (value.Length == 0)
// Switch on the string constant.
// case string.Empty:
Empty string example.
An empty string has 0 chars. These strings can be checked using a clear and efficient method. Developers frequently need to see if a string contains no characters.
Equality: If you look at == in the IL, it is called op_Equality. This operator compiles to a method call (like any other).
Length: We can do a Length check with an if-statement. FxCop, Microsoft's static analysis tool, recommends this method.
However: The Length check throws a NullReferenceException when the string is null. This condition occurs often in programs.Null StringsNullReferenceException
Also: We see the instance Equals method. The compiler must know the string is not null before calling the method.
C# program that uses empty strings
static void Main()
// Example string.
string str = "";
// Example tests.
if (str == "")
// Test with op_Equality
if (string.Equals(str, ""))
// Test with static Equals method
// Test with IsNullOrEmpty
if (str.Length == 0)
// Test with Length property
// Test with Equals method
Because string.Empty is initialized in the static constructor for the String class, it cannot be determined when you compile your program. It is a readonly field.Static ConstructorReadonly
Important: The case statements in switches can only contain const strings. So string.Empty, a readonly field, cannot be used.const
Notes, string switch.
Internally, complex switch statements are implemented as a Dictionary, providing constant lookup times. This is sometimes a speedup.String Switch
Here we look inside the .NET Framework and see how the Empty field is initialized. In the String class, look at the static constructor.
And: The Empty field is assigned to the constant "". This constant is a string literal instance.
Internal implementation of string.Empty:
Empty = "";
// More initialization omitted
Field declaration for string.Empty:
public static readonly string Empty;
A simple benchmark.
When you specify "" instead string.Empty, the C# compiler itself will know the value of the string data. The runtime is not a part of this decision.
Therefore: Certain C# compiler optimizations are effective only with "", not string.Empty.
Also: You can run a benchmark where string.Empty is much slower than "", by using a conditional that is removed by the C# compiler.
String.Empty benchmarked code:
if (string.Empty == null)
throw new Exception();
Constant string benchmarked code:
if ("" == null)
throw new Exception();
Second loop was completely compiled out before runtime.
string.Empty loop: 637 ms
"" loop: 319 ms [faster]
I ran these if-statements through 100 million iterations each. I did this in an effort to improve a program that had to use these tests frequently.
Results: The fastest way is to access the Length property. If you need to detect null, consider IsNullOrEmpty.
Note: Microsoft's FxCop warns when you use the other methods. It is right when it states that Length is the fastest method.
Equality operator ==: 796 ms
string.Equals: 811 ms
string.IsNullOrEmpty: 312 ms
Length: 140 ms [fastest]
Instance Equals: 1077 ms
Summary. An empty string can be checked in many ways. We choose which one depending on program context and preferences. The string.Empty field can be used.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.