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

查看:143
本文介绍了为什么我们不能有“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.


char类型被分类为一个
的整数类型,但它与
其他整数类型有两种不同:

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天全站免登陆