C# Empty String Examples

Use empty strings and the string.Empty field. Test the performance of empty strings.
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.


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 using System; class Program { static void Main() { // // Initialize string to empty string field. // string value = string.Empty; // // Test the empty field. // if (value == "") { Console.WriteLine("Empty"); } if (value.Length == 0) { Console.WriteLine("Empty"); } // // Switch on the string constant. // switch (value) { case "": { Console.WriteLine("Empty"); break; } // case string.Empty: // { // Console.WriteLine("Error!"); // break; // } } } } Output Empty Empty 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 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
Switch. 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.

Notes, string switch. Internally, complex switch statements are implemented as a Dictionary, providing constant lookup times. This is sometimes a speedup.String Switch
Implementation. 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: static String() { 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(); } Benchmark results: Second loop was completely compiled out before runtime. 1000000000 iterations. string.Empty loop: 637 ms "" loop: 319 ms [faster]
Benchmark. 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.

Benchmark results: 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
Dot Net Perls