Mike Flynn Mike Flynn - 1 year ago 90
C# Question

Better Way To Get Char Enum

Is there a cleaner way to get the char value of an enum in C#.

public enum DivisionStatus
{
None = 'N',
Active = 'A',
Inactive = 'I',
Waitlist = 'W'
}

string status = Enums.DivisionStatus.Active.ToString()[0].ToString(); // "A"

Answer Source

Just cast the value:

char status = (char)Enums.DivisionStatus.Active;

Note that this will use the value instead of the identifier. The Enums.DivisionStatus.Active value is the character code of 'A', as that is the value that you have defined.

Using the value directly is faster than looking up the identifier for the value.