Hi,In this article I will discuss the difference between string.Empty vs “”(Doublequotes).You will found enormous debate on this :
“Using an empty quoted string instantiates a new object. string.Empty does not.”
The point is that one thing should be clear in your mind of the 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.
"" will create a new string... it may create a new string once (possibly per AppDomain, possibly per process). For example, consider the below code:
public void Test()
{
string str1 = "";
for (int i=0; i < 1000; i++)
string str2 = "";
// Do Processing
}
string str3 = "";
string str4 = "";
Now str1, str2 ,str3 and str4 will refer to the same string.On invoking Test only one memory heap allocation takes place for empty string that is only at string str1 = "";For rest of the stuff where double quotes are used the reference of the above allocated memory heap is passed.Remove the misconception from your mind that it will take 1000 new strings.So in brief I would say "" is easy to write, easy to read.I prefer double quotes but it depends upon the consistency what your team follows. Double quotes will however create an object where String.Empty would not, but the "" will be reused throughout the life span of your application so the difference can be ignored.
And I remember John Skeet words.
"You won't create a new string each time - there'll be one string interned across the whole assembly. If you care about the extra 20 bytes (roughly) then .NET isn't for you ;) Go with whatever's more readable..." – Jon Skeet
Ya one thing more important"The value of a value type expression is the value, plain and simple. For instance, the value of the expression “2+3” is just 5. The valueof a reference type expression, however, is a reference. It’s not the object that the reference refers to. So, the value of the expression String.Empty is not an empty string—it’s a reference to an empty string." -Jon Skeet Happy Reading.:)
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