Loading ...

Difference between String.Empty and "" (doublequotes)

Who is online?  0 guests and 0 members
home  »  articles  »  Difference between String.Empty and "" (doublequotes)

Difference between String.Empty and "" (doublequotes)

(7382)
0
/5
Avg: 0/5: (0 votes)
Published: 7/26/2009 by  Raghav Khunger

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 value
of 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.:)

 

Comments (6)

Vinz
The difference is really minor. Initialization of values is really a matter of preference, but I've grown of using string.Empty over "" (double quotes) since it more cleaner to see in codes.. :)
7/27/2009
 · 
 
by
raghav_khunger
As i said above consistency of your team matters it is very very minute advantage of string.Empty:)
7/27/2009
 · 
 
by
hunterz85
Hardik Patel said:
String.Empty is more readable then double quotes
8/14/2009
 · 
 
by
rtpHarry
If you put String.Empty then you definitely meant to. If you put "" you might have forgotten something. For me its a no-brainer regardless of performance characteristics. It was good to read a bit more of a technical explanation though!
8/21/2009
 · 
 
by
 said:
wow, such an amazing discovery
2/26/2010
 · 
 
by
ramarthuluru
asdfadf aser said:

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

8/23/2010
 · 
 
by

Confirm

Product Spotlight

ASP.NET Hosting Spotlight

Most Recent Articles