tags:

views:

3327

answers:

8

In .NET, what is the difference between String.Empty and "", and are they interchangable, or is there some underlying reference or Localization issues around equality that String.Empty will ensure are not a problem?

+35  A: 

In .Net pre 2.0, "" creates an object while String.Empty creates no object. So it is more efficient to use String.Empty.

Source of information

.Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.

As @chadmyers mentioned, in version 2.0 and above of .Net, all occurrences of "" refer to the same string literal.

So "" is pretty equivalent to .Empty, but still not as fast as .Length == 0.

See the .Net specification for more information.

Brian R. Bondy
"" would only have created an object once anyway due to string interning. Basically the performance tradeoff is peanuts - readability is more important.
Jon Skeet
Interesting that even though the question says nothing about comparing a string to either "" or string.Empty to check for empty strings, a lot of people seem to interpret the question that way...
peSHIr
You mention `.Equals`. Did you mean to say `.Empty`?
strager
@strager: Hrm wrote this a year about but ya I think so. Changed.
Brian R. Bondy
+4  A: 

String.Empty does not create an object whereas "" does. The difference, as pointed out here, is trivial, however.

Esteban Araya
+2  A: 

All instances of "" are the same, interned string literal (or they should be). So you really won't be throwing a new object on the heap every time you use "" but just creating a reference to the same, interned object. Having said that, I prefer string.Empty. I think it makes code more readable.

Jason Jackson
+3  A: 

There appears to be some debate over whether there is a difference at all between String.Empty and "".

There is universal agreement, however, that if there is a difference, it is much too trivial to be concerned with.

MusiGenesis
+16  A: 

The previous answers were correct for .NET 1.1 (look at the date of the post they linked: 2003). As of .NET 2.0 and later, there is essentially no difference. The JIT will end up referencing the same object on the heap anyhow.

According to the C# specification, section 2.4.4.5: http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx

Each string literal does not necessarily result in a new string instance. When two or more string literals that are equivalent according to the string equality operator (Section 7.9.7) appear in the same assembly, these string literals refer to the same string instance.

Someone even mentions this in the comments of Brad Abram's post

In summary, the practical result of "" vs. String.Empty is nil. The JIT will figure it out in the end.

I have found, personally, that the JIT is way smarter than me and so I try not to get too clever with micro-compiler optimizations like that. The JIT will unfold for() loops, remove redundant code, inline methods, etc better and at more appropriate times than either I or the C# compiler could ever anticipate before hand. Let the JIT do its job :)

chadmyers
Small typo? "The JIT will end up referencing the same object on the help anyhow." Did you mean "on the heap"?
Dana
+2  A: 

The above answers are technically correct, but what you may really want to use, for best code readability and least chance of an exception is String.IsNullOrEmpty(s)

Eugene Katz
In terms of equality comparison, I completely agree, but the question was also about the difference between the two concepts as well as comparison
johnc
+3  A: 

String.Empty is a readonly field while "" is a const. This means you can't use String.Empty in a switch statement because it is not a constant.

James Newton-King