Why was the property
string foo = string.Empty included in the BCL? It seems more verbose and no clearer than just using an empty string (
string foo = "")
Why was the property
I can only assume here:
string.Empty has been defined for explicitness - when initializing a string, it may not be clear from context that
"" was indeed explicitly meant as an initializer (instead of
null or say
" " or just as a place holder during testing). Using
string.Empty is a definite answer to that sort of conundrum.
It may also be a throwback to C - an empty string in C is not an empty string. It is a character array whose first character is null (hence, empty), which is not the same as C#. My point here being that in different languages you would represent an empty string in different ways (and they may have different meanings) - having a
string.Empty precludes such ambiguity.
As opposed to what others say about multiple objects - this is not a problem as any string literal will get interned on compilation. This includes the value of
"". Any time either of these are repeated in code, the object will be retrieved from the intern pool. This is true per app domain.
I'm not 100% sure of the sources where I learned these, but some of the points for using it include:
Each string in a .NET assembly is unique, so having
string foo = ""; string bar = "";
results in 2 strings in the output assembly since strings are immutable. Having both reference
string.Emptyreduces assembly size.
- Explicitness. When you come across
string.Emptythe intent is clear that it's supposed to be an empty string. But if you come across
foo = ""did the programmer remove the contents of the string while testing and forget to add it back, or is it supposed to be that way?
No object will be created for
"" will create an object that will most likely come from the string intern pool.
In the past, people have run tests and
String.Empty comes out slightly faster, but its a micro-optimization.
String.Empty is this:
//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. public static readonly String Empty = "";
It is a matter of optimization of memory consumption and an optimization of string comparison. Every time you're using an empty string in your application, you are allocating a string object containing 0 characters. As for string comparison it can be done by comparing references (pointers) instead of character by character, which is a faster even for empty strings.
If you are using many times the same string in your application you can use the same kind of mechanism by calling String.Intern() with your string. But if you are using each string only once, then you'll only use more memory.
So String.Empty is only a special case optimization which is worth to do for most .Net applications, that's why it was integrated in the BCL.
For more detail on this subject I strongly recommend reading Eric Lippert's blog post.
You should also take a look at this documentation referenced by his blog post.