Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# return string.Empty; and return String.Empty; [duplicate]

Tags:

c#

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 "";
like image 709
GibboK Avatar asked Oct 01 '12 14:10

GibboK


2 Answers

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.

like image 124
Sergey Kalinichenko Avatar answered Oct 15 '22 05:10

Sergey Kalinichenko


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.

like image 43
Louis Kottmann Avatar answered Oct 15 '22 06:10

Louis Kottmann