7

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

What is the difference between:

return string.Empty;
return String.Empty;
return "";
Community
  • 1
  • 1
GibboK
  • 71,848
  • 143
  • 435
  • 658

2 Answers2

10

Since string is a language alias for String, there is absolutely no difference between the first and the second expressions.

Starting with .NET 2, "" returns exactly the same object as String.Empty, making all three statements exact equivalents of each other in terms of the value that they return. Even in .NET prior to 2 no multiple objects would be created due to interning.

The first and second snippets will produce IL code that is different from the third snippet, but they all would return the same object.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I thought the empty string literal results in different IL than using `String.Empty`. Quite insignificant, but I didn't think it was technically the "exactly the same object" or "exact equivalents". – Chris Sinclair Oct 01 '12 at 14:29
  • thanks for your answer, just for curiosity when make sense use ALIAS. so when it is common to use them? many thanks! – GibboK Oct 01 '12 at 14:32
  • @GibboK, using one vs. the other is mostly a matter of personal taste. My guess is the alias is there just to make things look a little more familiar to C++ programmers. Similarly, `int` is an alias for `Int32`, `long` is an alias for `Int64`, etc. – Tim Goodman Oct 01 '12 at 14:35
  • @GibboK I use `string` because that's the preference of my team; I would have no problem using `String` instead, though. – Sergey Kalinichenko Oct 01 '12 at 14:37
  • @ChrisSinclair That is a good point - I edited the answer to mention IL. – Sergey Kalinichenko Oct 01 '12 at 14:37
  • @dasblinkenlight Actually, reading into it a bit more, seems that when compiling for _release_ mode, the compiler will optimize them to the same assembly code. :) But I suppose that optimization implementation detail is up to the specific compiler/platform. – Chris Sinclair Oct 01 '12 at 14:44
  • Guys, what do you mean by IL code? Do you means CIL Common_Intermediate_Language?? – GibboK Oct 01 '12 at 16:54
  • 1
    @GibboK C# compiler converts your program to the *Internal Language* (IL) representation, the binary code of the .NET virtual machine. The answer [at this link](http://stackoverflow.com/a/1588678/335858) has the details. – Sergey Kalinichenko Oct 01 '12 at 17:01
5

There is, ultimately, no difference.

string is an alias for System.String (meaning it is literally the same thing), when you type "string", the compiler changes it to "System.String" at compilation.

Now, System.String.Empty is defined as:

public static readonly string Empty = "";

So it's just a convenient way of explicitely saying you want to use an empty string, instead of "" which could be a typo.

Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
  • 2
    personally I prefer constants like `Empty` in my code rather then a bunch of `string s ="";` – JonH Oct 01 '12 at 14:29