Mike Flynn Mike Flynn - 6 months ago 64
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"


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.