0

Possible Duplicate:
In C#, should I use string.Empty or String.Empty or “” ?

I just stumbled over the following sentence in this question/answer:

As of the C# 2.0 compiler, there is very little point to String.Empty anyway, and in fact in many cases it's a pessimisation, since the compiler can inline some references to "" but can't do the same with String.Empty.

Is this statement true? Should we therefore always use "" instead of string.Empty?

Please note: I'm not asking about any coding styleguide (which is easier to read). I'd like to know if there is a functional difference (e.g. inlining).

Community
  • 1
  • 1
M4N
  • 94,805
  • 45
  • 217
  • 260
  • 4
    http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or – Prisoner Jun 10 '11 at 14:00
  • I don't think it's necessary to make a distinction or worry about it either way. It's a waste of engineering/thinking time to consider the problem - there is no impact either way. (What I mean is: "it doesn't matter"). Is it harmful? Of course not, isn't that obvious? – Kieren Johnstone Jun 10 '11 at 14:01
  • If you look at the answer a little bellow (on that link) you will notice that the generated IL is almost the same and the generated assembly is exactly the same. – BrunoLM Jun 10 '11 at 14:05

3 Answers3

3

Is this statement true?

The point of interning is to share references to save memory and reduce comparison time (reference comparison is blazing fast). As String.Empty is a static readonly field on the String class, all uses of it already share the same reference. Therefore, String.Empty is already effectively interned (note that I said "effectively" and note, in particular, that it's not interned with uses of "" in your assembly).

Should we therefore always use "" instead of string.Empty?

Personally I find String.Empty to be vastly more readable. Use what you prefer. If there are any performance differences it is seriously unlikely to be a bottleneck.

jason
  • 236,483
  • 35
  • 423
  • 525
1
Console.WriteLine (ReferenceEquals ("", string.Empty) ? "Same Reference" : "Different Reference");
// Prints "Same Reference"

Looks like it's purely preference (at least on the Microsoft versions of the CLR). Does anyone know about MONO?

agent-j
  • 27,335
  • 5
  • 52
  • 79
0

You should almost always use the one that's easier to read. The potential will rarely matter in practice.

IMO "" is easier/clearer to read. But others prefer String.Empty. So it's mainly a matter of style.

The performance part you quotes seems plausible. "" is a compile-time constant and will be interned. String.Empty is a static readonly field. Unless the C# compiler programmers took special care to replace String.Empty with "" at compile-time it might be slightly slower in some rare scenarios. All uses of "" will share the same instance, and I expect String.Empty to point to the very same instance.

If you combine "" with other constants in a way that the compiler understands it might evaluate the whole expression at compile-time. It probably won't do that with String.Empty which is no constant. But I can't think of an example that makes sense in practice.

Example:

string s = ""+"A"+""+"B"+"";

might be faster than:

string s = string.Empty+"A"+string.Empty+"B"+string.Empty;

But of course any sane person would write:

string s = "AB";

But it's possible that the C# programmers added a special case for String.Empty that replaces it with "" in which case there would be no difference at all.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262