为什么你只能使用别名来声明枚举而不是.NET类型?

MrV*_*oid 11 .net c# enums types

这完美地工作..

public  enum NodeType : byte
{ Search, Analysis, Output, Input, Audio, Movement} 
Run Code Online (Sandbox Code Playgroud)

这会返回编译器错误...

public  enum NodeType : Byte
{ Search, Analysis, Output, Input, Audio, Movement} 
Run Code Online (Sandbox Code Playgroud)

使用反射时也是如此......

那么,有人知道为什么enum-base只是一个整体类型?

Pat*_*man 10

可能它只是一个不完整的编译器实现(虽然记录在案).

从技术上讲,这也应该有效,但事实并非如此.

using x = System.Byte;

public  enum NodeType : x
{ Search, Analysis, Output, Input, Audio, Movement}
Run Code Online (Sandbox Code Playgroud)

所以编译器的解析器部分只允许固定列表byte, sbyte, short, ushort, int, uint, long, or ulong.我没有技术限制.


Sel*_*enç 5

因为规格如此说:

enum-declaration:
attributesopt enum-modifiersopt enum identifier enum-baseopt enum-body ;opt

enum-base:
: integral-type

enum-body:
{ enum-member-declarationsopt }
{ enum-member-declarations , }

Each enum type has a corresponding integral type called the underlying type of the enum type. This underlying type must be able to represent all the enumerator values defined in the enumeration. An enum declaration may explicitly declare an underlying type of byte, sbyte, short, ushort, int, uint, long or ulong. Note that char cannot be used as an underlying type. An enum declaration that does not explicitly declare an underlying type has an underlying type of int.

...

积分型定义为,

integral-type:
sbyte
byte
short
ushort
int
uint
long
ulong
char