Possible Duplicate:
What is the difference between String.Empty and “”
Which of the following should I use for assigning an empty string and why?
string variableName = "";
or
string variableName = string.Empty;
String.Empty
is what is normally recommended.
Why? Because it conveys meaning, you are very explicitly saying that you want the string to be empty.
Additionally, ""
creates a string object (this object will be reused across the applications, because strings in .NET are interned), String.Empty
does not create a new object. See here.
Use whatever your project or team style guidelines say. And if you have none, just chat about it for a moment, choose one and use it consistently.
Technically both are identical and lead to the same code. String.Empty represents instance of interned empty string (""). And since all in-code string constants are automatically interned, "" represents exactly the same instance of empty string (Update: Within assembly, see update below).
And by the way, I'm using "". It's short, clear and readable.
String.Empty is useful in generic code using reflection etc. When you don't care what class you have, just want to get empty value for e.g. initialization with default value.
Update: It appears that it's not that simple. There might be many instances of interned string within single app domain. And the behavior depends on framework version. For more information see Eric Lippert's String interning and String.Empty and remarks in Reference for String.Intern method.
But the second link suggest that since 3.5 SP1 there is again single interned "", as it should be in the first place. So, why bother with it when you program? Projects take some time to complete. Factor in improvements in compilers, frameworks and tools... And don't optimize prematurely. It's more important to be readable, consistent and correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With