Why we can't have “char” enum types

2019-01-18 08:30发布

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?

标签: c# enums char
8条回答
倾城 Initia
2楼-- · 2019-01-18 08:41

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.

查看更多
地球回转人心会变
3楼-- · 2019-01-18 08:44

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:

public enum CharEnum
{
    one = '1'
}

You can do this, however, by using ASCII byte values and then converting:

public enum CharEnum
{
    one = 49,
    two = 50
}

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. :-)

查看更多
相关推荐>>
4楼-- · 2019-01-18 08:53
char charPC = 'P';
if (Enum.IsDefined(typeof(PayCode), (PayCode)charPC)) { 
        // check if charPC is a valid value
        PayCode enumPC = (PayCode)charPC; // enumPC == PayCode.Paid
}
  • Made a small change, no need to convert it to an (int) in the if for all cases, depends on the enum type. But if you cast it directly to the enum type itself, it should always work.
查看更多
我只想做你的唯一
5楼-- · 2019-01-18 08:57

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

查看更多
Root(大扎)
6楼-- · 2019-01-18 09:00

This is workaround I'm using

enum MyEnum
{
    AA = 'A',
    BB = 'B',
    CC = 'C'
};

static void Main(string[] args)
{
    MyEnum e = MyEnum.AA;
    char value = (char)e.GetHashCode(); //value = 'A'
}
查看更多
再贱就再见
7楼-- · 2019-01-18 09:04

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.

查看更多
登录 后发表回答