Thursday, September 15, 2011

string.Empty vs ""

Regarding string.Empty vs "". string.Empty is performant(using this word comes with a risk of sounding like a sales brochure) than "", but that is only prior .NET 2.0. Prior .NET 2.0 (i.e. 1.1 and 1.0), every literal strings even they are similar, are allocated their own slots in memory, hence declaring "" once and pre-assigning it in string.Empty is warranted and thus faster and saves space than merely using "" strings everywhere. string.Empty is faster than "", but that is on .NET 1.0 and 1.1 only

But on .NET 2.0 onwards, every similar literal strings are interned.

Interning is the process of looking at all similar string literals in an executable and making them point to one memory address only, i.e. all similar string literals share the same address.

On .NET 2.0 and onwards, every similar empty strings(any similar strings for that matter, even non-empty ones) doesn't allocate new memory

string.Empty is still nice, it is readable and one can easily glean the code intent. But that is debatable, I believe that "" is more readable than string.Empty. In the same vein that 0 is readable than int.Zero. Any takers of this code? if(i == int.Zero)

And string.Empty could be slower than "" on 2.0 and onwards, string.Empty is another indirection during runtime. "" is just that, a memory address has just one location, never-changing.



using System; 

namespace StringInterning 
{ 
    class Program 
    { 
        unsafe static void Main(string[] args) 
        { 
            string s = "Hello"; 
            string t = "Hello"; 
             

            fixed (char* dst = s) 
            { 
                dst[0] = 'M'; 
                dst[1] = 'i'; 
            } 
             

            Console.Write("Enter greeting: "); 
            string greet = Console.ReadLine(); // try to enter the word: Hello 
            Console.WriteLine(); 


            if (greet == "Hello") 
                Console.WriteLine("Friendly"); // if you enter Hello for greet, this will not print. funneh :D 
            else 
                Console.WriteLine("This will print instead"); // this will print instead 

            Console.WriteLine("S {0}", s); // will print Millo 
            Console.WriteLine("T {0}", t); // will print Millo 
            Console.WriteLine("Hey {0}", "Hello"); // will print Hey Millo 

            Console.WriteLine(); 

            fixed (char* dst = " ") 
            { 
                *dst = 'Y'; 
            } 

            Console.WriteLine("Value of {0}{1}", " ", " "); // this will print YY. funneh :D 


            Console.ReadLine(); 
        } 
    } 

} 

Enter greeting: Hello 

This will print instead 
S Millo 
T Millo 
Hey Millo 

Value of YY 


And the great Jon Skeet uses.., surprise! --> ""

here's his rationale:

http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or/263257#263257


We can't even use string.Empty on parameters, it will not compile:
public static void Funny(string message = string.Empty) // this will not compile. must use ""
{
    Console.WriteLine("Hahah {0}", message);
}


string.Empty can't even be used on attributes, it will not compile:
[Label(string.Empty)] // this will not compile. must use ""


string.Empty can't even be used on switch, it will not compile:
string s = Console.ReadLine();
switch (s) 
{
case "Hahah":
    break;

case string.Empty: // this will not compile too. must use ""
    break;
}



For those who are dogmatic on using string.Empty. Though it's not your problem, it's the language designer's choice, you have to do some concessions and you shall just write "" anyway.



Perhaps programmers using string.Empty love to code so much ツ

No comments:

Post a Comment