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?
|
I think in most cases there is no difference. In normal cases, when you use If you want proof:
The above code prints Bottom line: the difference is personal taste. |
|||||||||||||
|
I don't think there is any performance gain in using string.Empty over "". Its just a matter of personal preference. |
|||||||||||||||||||||
|
string.Empty is a singleton static const string that has been constructed, but "" will create a new string that is empty. |
|||||||||||||||||||||
|
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)
|
|||||
|
|
|||
|
If you fancy reading some assembler based evidence of this the following post could be interesting: http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or/1588678#1588678 Actually, there are some interesting answers in that whole thread. |
|||
|
Pre 2.0, it was correct that |
|||
|