An enum declaration declares a new enum type. An enum declaration begins
with the keyword enum, and
defines the name, accessibility, underlying type, and members of the enum.
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: char cannot be used as an underlying type. end note] An
enum declaration that does not
explicitly declare an underlying type has an underlying type of int.
[Example: The example
enum Color: long
{
Red,
Green,
Blue
}
declares an enum with an underlying type of long. end example] [Note: A
developer might choose to use an
underlying type of long, as in the example, to enable the use of values
that are in the range of long but not
in the range of int, or to preserve this option for the future. end note]
with the keyword enum, and
defines the name, accessibility, underlying type, and members of the enum.
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: char cannot be used as an underlying type. end note] An
enum declaration that does not
explicitly declare an underlying type has an underlying type of int.
[Example: The example
enum Color: long
{
Red,
Green,
Blue
}
declares an enum with an underlying type of long. end example] [Note: A
developer might choose to use an
underlying type of long, as in the example, to enable the use of values
that are in the range of long but not
in the range of int, or to preserve this option for the future. end note]
本文详细介绍了枚举类型的定义、语法及使用方式。包括如何声明枚举类型、指定底层整数类型以及枚举成员的定义等关键概念。
2596

被折叠的 条评论
为什么被折叠?



