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 defau
Targeting VB.NET and CLI consumers, you can compile the char enum like this (easiest with a VB.NET or C# project that has IL Support (an extension) enabled):
.class public auto ansi sealed Hafner.Testing.EdgeCase.CharEnum
extends [mscorlib]System.Enum
{
.field public specialname rtspecialname char value__
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum Min = char(0)
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum Zero = char(0)
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum Max = char(65535)
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum DuplicateSameValue = char(65) //A
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum duplicateSameValue = char(65) //A
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum DuplicateOtherValue = char(90) //Z
.field public static literal valuetype Hafner.Testing.EdgeCase.CharEnum duplicateOtherValue = char(88) //X
}
The default type is int. More information at the C# reference at MSDN. You can also find a link to the C# language specification at MSDN. I think the reason for the restriction probably derives from these statements in the language specification, section 4.1.5.
The char type is classified as an integral type, but it differs from the other integral types in two ways:
• There are no implicit conversions from other types to the char type. In particular, even though the sbyte, byte, and ushort types have ranges of values that are fully representable using the char type, implicit conversions from sbyte, byte, or ushort to char do not exist.
• Constants of the char type must be written as character-literals or as integer-literals in combination with a cast to type char. For example, (char)10 is the same as '\x000A'.
char charPC = 'P';
if (Enum.IsDefined(typeof(PayCode), (PayCode)charPC)) {
// check if charPC is a valid value
PayCode enumPC = (PayCode)charPC; // enumPC == PayCode.Paid
}