C# string.Empty Example

Dot Net Perls
String type

You are wondering how to use string.Empty and how it is different from an empty string literal constant "". There is a subtle different, and it can be significant in some scenarios, in that it changes the meaning of your program. We see how to use string.Empty, and then look into its implementation.

Example

First, here we see some examples of how you can use string.Empty and "" in a C# program. the string.Empty field is initialized to "" at run-time by the framework. This means it cannot be used in a switch, which we see in the example. However, you can test with string.Empty in conditionals.

Program that uses string.Empty [C#]

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

Description. 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 on the switch. You cannot use string.Empty as a switch case, because it cannot be determined at compile time by the C# compiler. This is an example of how string.Empty is a needlessly disabled version of "".

Switches and string.Empty

Switch illustration

Because string.Empty is initialized in the static constructor for the String class, it cannot be determined when you compile your C# program. This is because it is a readonly field. The case statements in switches can only contain const strings.

Readonly Fields

Switching performance impact. Internally, more complex switch statements are implemented as a Dictionary, providing constant lookup times. This is a massive performance boost in some cases. If you choose to use if conditions instead of switch because they work with string.Empty, you could lose this optimization.

String Switch Examples

Internal implementation

.NET Framework information

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 and you can find more specific details about string literals.

String Literal
Internal implementation of string.Empty

static String()
{
    Empty = "";
    //
    // More initialization omitted
    //
}

Field declaration for string.Empty

public static readonly string Empty;

Advantages of ""

Programming tip

When you specify "" instead string.Empty, the C# compiler itself will know what the value is. The runtime is not a part of this decision, and this occurs before JIT happens. Therefore, certain C# compiler optimizations are effective only with "", not string.Empty. 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. Additionally, we found that it provides less information to the C# compiler about your code 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.

String Type