Greg Greg - 11 months ago 37
C# Question

Why can't String.Empty be used when creating a const string variable?

I often use

for a placeholder, which equates to
. So my question, why does a
not like
? They compile identically.

// Good
private const string example = "";

// Bad
private const string example = String.Empty;

What in
makes it not able to be used with a

Answer Source

String.Empty is not a constant it's only readonly field. And since it's readonly field (which is not known on compile time) it can't be assigned to a constant.,c9f70a27facb27cf

//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;