Why we can't have “char” enum types

前端 未结 9 1012
不知归路
不知归路 2020-12-06 03:50

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

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 04:49

    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'.

提交回复
热议问题