Greg Greg - 1 year ago 106
C# Question

A const and a string

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download