String.Empty. The string.Empty field is an empty string literal. 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.

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.

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.

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.

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.