C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It is slightly different from an empty string literal constant "". There is a subtle difference—but one that could be significant in some scenarios. It changes the meaning of a program.
Example. We use string.Empty and "" in a C# program. The string.Empty field is initialized to "" at runtime by the .NET Framework. This means it cannot be used in a switch. But you can test with string.Empty in if-statements.
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
The example assigns the string variable "value" to string.Empty. This is mostly equivalent to "". Next, it tests that variable against the constant "", which returns true, and finally it tests the Length and switches on the value.
Note: You cannot use string.Empty as a switch case, because it cannot be determined at compile-time by the C# compiler.
And: This is an example of how string.Empty is a needlessly disabled version of "".
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. The case statements in switches can only contain const strings.
Static ConstructorReadonlyConst
Internally, complex switch statements are implemented as a Dictionary, providing constant lookup times. This is sometimes a speedup. If you use if-conditions because they work with string.Empty, you could lose this optimization.
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. 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;
Discussion. 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. This occurs before the execution engine is started and before JIT happens.
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]
Summary. Here we looked in depth at string.Empty, first seeing how to use it in your program and then reviewing why it won't work in switches. We also found that it provides less information to the C# compiler than the constant itself.
Finally: We saw that the C# compiler can completely eliminate certain instructions that use "", but not those that use string.Empty.