C# string.Empty vs “”

As we know we have two ways to set a variable value to an empty string.
We can just set it to empty quotes “” or set it to string.Empty, but what is the difference and is there one at all ?

If we will look at the .NET String.cs we will see the next line:

        public static readonly String Empty = "";

The above line can tell us that string.Empty is not a constant as we would like to believe but a readonly field.

The question is why, why static readonly and not a constant. The answer is in the same String.cs comments of the Empty field.

        // The Empty constant holds the empty string value.
        //We need to call the String constructor so that the compiler doesn't mark this as a literal.
        //Marking this as a literal would mean that it doesn't show up as a field which we can access
        //from native. 

Now, let’s make some test.

            string EmptyValue = string.Empty;

            if (EmptyValue == "")
                // Output : Is Empty : 1
                Console.WriteLine("Is Empty : 1");

            if (EmptyValue.Length == 0)
                // Output : Is Empty : 2
                Console.WriteLine("Is Empty : 2");

            switch (EmptyValue)
                case "":
                        // Output : Is Empty : 3
                        Console.WriteLine("Is Empty : 3");
                 case string.Empty:
                        // Compilation error : A constant value is expected
                        Console.WriteLine("A constant value is expected");

            if (EmptyValue.Equals (""))
                // Output : Is Empty : 4
                Console.WriteLine("Is Empty : 4");

            if (string.IsNullOrEmpty(EmptyValue))
                // Output : Is Empty : 5
                Console.WriteLine("Is Empty : 5");

As we can see string.Empty and “” have the same behavior except the “switch” because string.Empty is not a constant.

And of course the most important question – is there a performance differences between the two ? The answer is – Yes, but minor.
Lets see some results :

           // Execution Time : 2144ms 
            for (int n = 0; n < 100000000; n++)
                if (TestValue == string.Empty)
                    t = 1; //"For test purposes only..."

            // Execution Time : 2211ms
            for (int i = 0; i < 100000000; i++)
                if (TestValue == "")
                    t = 2; //"For test purposes only..."

We can see from the results that the comparison with string.Empty is a little bit faster but it's negligible.

One of the advantages of using "" is that it's in some cases it can be optimized by the compiler before the JIT. For example if you will run the same iteration while comparing ""==null the whole block will be removed by the optimizer.

So, use what ever you like, you'd probably won't see the difference.

VN:F [1.9.22_1171]
Rating: 4.2/5 (9 votes cast)
C# string.Empty vs "" , 4.2 out of 5 based on 9 ratings
  1. Lasse V. Karlsen
    July 13th, 2009 at 14:51 | #1

    Are you sure there is a difference except your runtime environment in that last test there?

    Here’s the two comparisons, disassembled in release-mode:

    if (TestValue == string.Empty)
    00000036 mov edx,dword ptr ds:[022F102Ch]
    0000003c mov ecx,dword ptr [ebp-10h]
    0000003f call 75D95F98

    if (TestValue == “”)
    00000059 mov edx,dword ptr ds:[022F202Ch]
    0000005f mov ecx,dword ptr [ebp-10h]
    00000062 call 75D95F98

    In debug mode there’s more code, but apart from the two constants in the first assembler line, there’s no difference. To me it looks like identical pieces of code.

    What am I doing wrong?

    VA:F [1.9.22_1171]
    Rating: 3.4/5 (5 votes cast)
  2. July 13th, 2009 at 16:10 | #2

    @Lasse V. Karlsen
    This is what i see from IL

    empty quotes (“”) Test :
    IL_001c: ldloc.0
    IL_001d: ldstr “”
    IL_0022: call bool [mscorlib]System.String::op_Equality(string,string)

    string.Empty Test :
    IL_001c: ldloc.0
    IL_001d: ldsfld string [mscorlib]System.String::Empty
    IL_0022: call bool [mscorlib]System.String::op_Equality(string,string)

    As you can see there is a difference.
    “” – represented as “ldstr” – pushes an object reference (type O) to a new string object

    string.Empty – represented as “ldsfld” – pushes the value of a static field on the stack

    VN:F [1.9.22_1171]
    Rating: 4.2/5 (5 votes cast)
  3. ASDF
    August 24th, 2010 at 01:58 | #3

    One interesting thing I observed when compiling with Visual studio.

    If a variable is assigned with “” and not used in the code, then VS will throw warning.

    If a variable is assigned with string.Empty and not used in the code, then VS will NOT throw warning.

    I think “” is helpful to find out unused variables in the application

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  1. July 13th, 2009 at 20:01 | #1

Subscribe without commenting

SEO Powered by Platinum SEO from Techblissonline
watch free movies