Two ways to get an Enum. Consider the following Enum:
public enum MyEnumType { Default = 0, First, Second, Third, }
How to get an Enum type from a string equivalent to the Enum value?
(MyEnumType)Enum.ToObject(typeof(MyEnumType), Convert.ToInt16('1'));
// Gives you MyEnumType.First
How to get an Enum type from a string equivalent to the Enum name?
(MyEnumType)Enum.Parse(typeof(MyEnumType), 'Second', true);
//passing in true as the third parameter makes it not case-sensitive
// Gives you MyEnumType.Second
How to get a value from an Enum?
int enumVal = (int)MyEnumType;
Leave a comment