Why we can't have "char" enum types

Praveen Sharma picture Praveen Sharma · Feb 21, 2009 · Viewed 41.5k times · Source

I want to know why we can't have "char" as underlying enum type. As we have byte,sbyte,int,uint,long,ulong,short,ushort as underlying enum type. Second what is the default underlying type of an enum?

Answer

kad81 picture kad81 · Apr 18, 2012

I know this is an older question, but this information would have been helpful to me:

It appears that there is no problem using char as the value type for enums in C# .NET 4.0 (possibly even 3.5, but I haven't tested this). Here's what I've done, and it completely works:

public enum PayCode {
    NotPaid = 'N',
    Paid = 'P'
}

Convert Enum to char:

PayCode enumPC = PayCode.NotPaid;
char charPC = (char)enumPC; // charPC == 'N'

Convert char to Enum:

char charPC = 'P';
if (Enum.IsDefined(typeof(PayCode), (int)charPC)) { // check if charPC is a valid value
    PayCode enumPC = (PayCode)charPC; // enumPC == PayCode.Paid
}

Works like a charm, just as you would expect from the char type!