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?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Character enums would simply be strings wouldn't they? I'm not sure what other benefit you would derive from a character enumeration?
As others have said, the default type is int for an enumeration.
Technically, you can't do this. But, you can convert the enum to a byte and then convert that to char. This is useful if your goal is to have something like this (realizing this is impossible to do:
You can do this, however, by using ASCII byte values and then converting:
You can then convert to byte and to char to get the char value. It is not really pretty, but it will work, if getting a char is your ultimate goal. You can also use unicode and an int value, if you need chars outside of the ASCII range. :-)
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.
This is workaround I'm using
See ECMA standard 335, Common Language Infrastructure (CLI), in Ecma International. The CLI allows the underlying type to be char or bool but C# and VB.Net don't allow it. For what it is worth, C++/CLI does allow System::Char as the underlying type.
I presume that C# and VB.Net don't allow char and bool as the underlying type for syntactical reasons only.