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?

0

1 Answer 1

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 = ""; 
Sign up to request clarification or add additional context in comments.

1 Comment

Interestingly, the comments say "we need to call the string constructor" but the code does not call the string constructor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.