How do I do a simple compare of an enum value and a string that should match the enums name?
How do I parse the string into it’s appropriate enum value.
For example,
Enum A B=0 C=1 D=2 End Enum
How do I check if String = A.C and how do I convert string into its corresponding A value without comparing it to a string representation?
There are several different methods that are related:
The first two convert the value of
A.Cto a string representation ('C') and then compare it to a string. The last one converts the string'C'to a typeA, and then compares as an actual typeA.Enum to string:
enumValue.ToString()orEnum.GetName(typeof(A), A.C)String to enum:
(A)Enum.Parse(typeof(A), 'C')Note that none of those will really work if the enumeration is marked with
FlagsAttribute.