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 "";
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 "";
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.
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.