为什么我们不能有“char”枚举类型 [英] Why we can't have "char" enum types

查看:707
本文介绍了为什么我们不能有“char”枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我们不能有char作为底层枚举类型。
因为我们有byte,sbyte,int,uint,long,ulong,short,ushort作为底层的
枚举类型。
第二个枚举的默认底层类型是什么?

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?

推荐答案

默认类型为int。有关详细信息,请参阅MSDN上的C#参考。您还可以在MSDN中找到 C#语言规范的链接。我认为限制的原因可能源自语言规范第4.1.5节中的这些语句。

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:

•没有隐式转换
其他类型为char类型。在
特殊情况下,尽管sbyte,
字节和ushort类型具有可以完全表示的
值的范围
使用char类型,隐式
从sbyte转换,byte或
ushort to char不存在。

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

•char类型的常量必须是
,以字符文字或
整数文字与
组合转换为char类型。例如,
(char)10与\x000A相同。

• 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”枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆