问题描述
我要宣布一个新的枚举与非默认的基本类型。这工作:
I want to declare a new enum with non-default underlying type. This works:
public enum MyEnum : short
{ A, B, C, }
但我不明白为什么这不能编译:
But I don't understand the reason why this doesn't compile:
public enum MyEnum : System.Int16
{ A, B, C, }
编译器说:
类型字节,为sbyte,总之,USHORT,INT, UINT,长或ulong预期
据我所知,总之是所有的.NET版本(32/64位口味在内)的别名Int16的。我不明白为什么编译器给出了不同的含义,在特定情况下的别名。
I understand that short is an alias for Int16 on all .NET versions (32/64 bit flavors included). I don't see why the compiler gives a different meaning to the alias in that particular case.
任何解释?
推荐答案
的语法是正确的。 C#规范明确指出,枚举的基础类型必须是字节,为sbyte,总之,USHORT,INT,UINT,长或ulong的。
The syntax is correct. C# specification explicitly states that the enum's underlying type must be byte, sbyte, short, ushort, int, uint, long or ulong.
读什么微软表示,这个这里。
Read what Microsoft says about this here.
这篇关于为什么枚举声明接受短暂而不是Int16的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!