簡體   English   中英

為什么你只能使用別名來聲明枚舉而不是.NET類型?

[英]Why can you use just the alias to declare a enum and not the .NET type?

這完美地工作..

public  enum NodeType : byte
{ Search, Analysis, Output, Input, Audio, Movement} 

這會返回編譯器錯誤...

public  enum NodeType : Byte
{ Search, Analysis, Output, Input, Audio, Movement} 

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

那么,有人知道為什么enum -base只是一個整體類型?

可能它只是一個不完整的編譯器實現(雖然記錄在案)。

從技術上講,這也應該有效,但事實並非如此。

using x = System.Byte;

public  enum NodeType : x
{ Search, Analysis, Output, Input, Audio, Movement}

所以編譯器的解析器部分只允許固定列表byte, sbyte, short, ushort, int, uint, long, or ulong 我沒有技術限制。

因為規格如此說:

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

字節,Int32等是對象。 由於枚舉需要整數類型而這些不是,因此會出現編譯錯誤。 在這方面,C#的枚舉定義更接近於C語言。

這與Java非常不同,因為它們實際上被稱為單例,因此枚舉是用詞不當。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM