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

查看:26
本文介绍了为什么我们不能有“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,byte 和 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 类型的常量必须是写成字符文字或作为整数文字与 a 组合强制转换为字符类型.例如,(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天全站免登陆