1

Possible Duplicate:
Why isn't String.Empty a constant?

I can use "" but not string.Empty when specifying default values for method arguments in C# 4.0. This would make sense if string.Empty could mean something other than "", but it is a constant, it is just not declared as const.

Was this just a mistake on Microsoft's part that they can't clean up with const because it is too core to the language and would be a breaking change? Or was there a legitimate reason for making this a populated field instead of a const in the first place?

Community
  • 1
  • 1
Jon Davis
  • 6,562
  • 5
  • 43
  • 60

1 Answers1

4

It can't be a literal, or it wouldn't be accessible from the native half of String.

The comments (in the reference source) say:

// The Empty constant holds the empty string value.
//We need to call the String constructor so that the compiler doesn't mark this as a literal.
//Marking this as a literal would mean that it doesn't show up as a field which we can access
//from native. 
public static readonly String Empty = "";
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Interestingly, the comments say "we need to call the string constructor" but the code does not call the string constructor. – phoog Dec 12 '12 at 04:27