I have seen this generally handled by putting a [StringValue("New York")]
attribute on the enum members. A quick google search returns this blog post, which has a pretty good way of doing it.
Basically make the attribute class:
public class StringValueAttribute : Attribute {
public string StringValue { get; protected set; }
public StringValueAttribute(string value) {
this.StringValue = value;
}
}
And an extension method to access it:
public static string GetStringValue(this Enum value) {
// Get the type
Type type = value.GetType();
// Get fieldinfo for this type
FieldInfo fieldInfo = type.GetField(value.ToString());
// Get the stringvalue attributes
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];
// Return the first if there was a match, or enum value if no match
return attribs.Length > 0 ? attribs[0].StringValue : value.ToString();
}
Then your enum would look like this:
public enum EnumState{
[StringValue("New York")]
NewYork,
[StringValue("New Mexico")]
NewMexico,
}
and you can just use myState.GetStringValue();