I am certain that string.Empty is more efficient than using "" in .Net languages. What I would like to know is WHY is it more efficient?
-
8what does make you think it's more efficient? – Gregory Pakosz Dec 11 '09 at 11:31
-
@Gregory - StyleCop recommends making the change from "" to string.Empty. I like to think there is some reasoning behind this... – JL. Dec 11 '09 at 11:34
-
3But what makes you think the reasoning has to do with efficiency? – jalf Dec 11 '09 at 11:39
-
6And what makes you think that StyleCop is always right, and always up-to-date in its recommendations? – Jon Skeet Dec 11 '09 at 11:48
-
It is not. See https://youtu.be/qWBi32-Njm8 – TimTIM Wong Nov 06 '22 at 00:15
5 Answers
I think in most cases there is no difference. In normal cases, when you use ""
in your code this string will be interned and the same string instance will be reused. So there will not be more string instances around when using ""
as compared to String.Empty
.
If you want proof:
Dim a As String
Dim b As String
a = ""
b = ""
Console.WriteLine(Object.ReferenceEquals(a, b)) ' Prints True '
The above code prints True
also if you replace one of them with String.Empty
, so you can even mix the approaches without creating extra string instances.
Bottom line: the difference is personal taste.

- 155,851
- 29
- 291
- 343
-
Is there any overhead accessing an interned string compared to accessing a puplic static string? – JonoW Dec 11 '09 at 11:38
-
1Ok, accepted answer. This is good to know, because a lot of IT geeks frown upon the use of "". I guess this is another StyleCop rule worth ignoring too. – JL. Dec 11 '09 at 11:38
-
@JonoW: never measured it, but it would surprise me if there is any difference. – Fredrik Mörk Dec 11 '09 at 11:40
First of all, I'm not sure that it is more efficient. A reason it is a static is, it is basically a constant: there is only one empty string.
A more important issue is that there are some inconsistencies with string interning in .NET, which can result in comparisons with String.empty not always returning the expected result, depending on what version of the runtime you're using. Eric Lippert has written a fascinating blog post about it here.
(Example taken from Eric's post)
object obj = "";
string str1 = "";
string str2 = String.Empty;
Console.WriteLine(obj == str1); // true
Console.WriteLine(str1 == str2); // true
Console.WriteLine(obj == str2); // sometimes true, sometimes false?!

- 28,507
- 14
- 48
- 67
-
1That third line will never be false. Perhaps you mean object.ReferenceEquals? – John Gietzen Feb 20 '14 at 16:40
I don't think there is any performance gain in using string.Empty over "".
Its just a matter of personal preference.

- 184,426
- 49
- 232
- 263
-
2Agree, but `string.Empty` is definitely more explicit than "" – Klaus Byskov Pedersen Dec 11 '09 at 11:29
-
1Stylecop recommends using string.Empty, surely this is not just because it is more visually appealing than ""? – JL. Dec 11 '09 at 11:29
-
3Sebastian - incorrect. If "" is in code, then all references to "" will be interned to the same instance, just like any other string constant – thecoop Dec 11 '09 at 11:32
""
will create an instance of an empty string in your application. Sure, .NET interns the string constants, but still, you will create one instance. String.Empty
, on the other hand, does not create any instance.

- 49,681
- 17
- 108
- 138
string.Empty is a singleton static const string that has been constructed, but "" will create a new string that is empty.

- 8,547
- 9
- 60
- 93
-
4...which also means that it's interned - there's only one copy of the string in memory regardless of how many times it's used. – ctford Dec 11 '09 at 11:30
-
-
4It won't create a new instance every time you reference "", it will use a reference to the single interned value of "" – thecoop Dec 11 '09 at 11:33
-
2wrong, look up string interning. If you use: `new string("")` however, you'll use more memory... – Rob Fonseca-Ensor Dec 11 '09 at 11:35
-
-
2If you use `new string("")` you'll get a compile-time error - there isn't a string constructor taking just a string. – Jon Skeet Dec 11 '09 at 11:49