Why we can’t have “char” enum types

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!

Leave a Comment

tech