It doesn't matter, it's not any difference as described in MSDN article.
String.Empty Field
The value of this field is the zero-length string, "".
It doesn't matter, it's not any difference as described in MSDN article.
String.Empty Field
The value of this field is the zero-length string, "".
Its good to use String.Empty rather than ""
myString = “” creates a new instance of type String.
myString = String.Empty does not.
EDIT
Misconception that using an empty quoted string always instantiates a new object.The question in your mind will be arising that why I am saying like that . Note that .NET interns its strings, so future instances will pull the same immutable string from the intern pool, and any performance hit will be negligible. In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool. The single copy of each string is called its 'intern' and is typically looked up by a method of the string class.What that means is ok if “” creates object while String.Empty does not. But this object will be created once and will be referenced from the string pool later if you have another "" in the code. String interning keeps a hashtable of strings while running an application. If a string with the same contents is created, no new heap allocation happens but instead a reference to the existing (identical) string is returned.Keep in mind each string literal does not necessarily result in a new string instance.
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.
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.
A good article explaining the differences between ""
and string.Empty
can be found here.
From the article:
"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"
From what I've read there's no difference in behavior, and very, very little difference in efficiency between the two (with the very slight edge going to string.Empty). So it doesn't really matter which one you choose, there's no practical difference.
Better to use string.Empty
than "".
string.Empty
is more readable code than "".
better to use string variableName = string.Empty;
not string variableName = "";
Note: there is not object creation for ""
.Net 3.0 and above.Both "" and String.Empty are same.
But in .Net 2.0 ""
will create a new instance and not for String.Empty
http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or
http://www.ecma-international.org/publications/standards/Ecma-334.htm
" "
and ""
look similar. if you cannot catch a space inside " "
by mistake
then " "
can pass String.IsNullOrEmpty()
. and make unexpected result.