0

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

Is it better to

return string.Empty;

instead of

return "";

What do you think?

Community
  • 1
  • 1
msfanboy
  • 5,273
  • 13
  • 69
  • 120

3 Answers3

7

The two are exactly identical once compiled. I find string.Empty is more readable. For all we know "" really should have something inside of it and it's just a typo.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
3

According to this MSDN blog post string.Empty is more efficient as it will not produce a new object.

Here are some more information, including a short performance comparison.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • Yeah but it is pre .NET 2.0: http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and ;-) – MartyIX Aug 20 '10 at 20:50
  • The blog post is not correct. The string literal is created only once when the assembly is loaded (just like the string object for the String.Empty property), executing the code that uses the string literal doesn't create an object. – Guffa Aug 20 '10 at 20:53
1

Once the compiler gets done with that code, the IL/machine code for both fragments will be identical.

I tend to use string.Empty when an empty string is an important part of the algorithm. I tend to use "" for values that could be anything but for now it just happens to be an empty string. In other words, use the one that communicates the intent of the expression.

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99