The Body of An Enum Type Declaration Defines Zero or More Enum MEMBERS,
Which is the named constants
Of The Enum Type. No Two Enum Members Can Have The Same Name.
Enum-member-declarations:
ENUM-Member-Declaration
ENUM-Member-Declarations, Enum-Member-Declaration
Enum-member-declaration:
AttributeSopt Identifier
AttributeSopt Identifier = Constant-Expression
Each ENUM Member Has An Associated Constant Value. The Type of this Value IS
The underlying type for the the Underlying Type for THE
Containing enum. The Constant Value for Each ENUM Member Must Be in The CONSTANT
Range of the underlying type for
The enum. [Example: The Example
ENUM color: uint
{
RED = -1,
Green = -2,
Blue = -3
}
RESULTS IN A Compile-Time Error Because The constant value -1, -2, and? 3
Are Not in the Range of the
underlying Integral Type uint. end esample]
Multiple Enum Members May Share The Same Associated Value. [Example: The
EXAMPLE
ENUM Color
{
RED,
Green,
Blue,
Max = blue
}
Shows An Enum That Has Two ENUM MEMBERS? Blue and max? That Have the Same
Associated value. End
EXAMPLE]
The Associated Value of An Enum Member Is Assigned Either Implicitly OR
EXPLICITLY. IF The Declaration of the
ENUM MEMBER HAS A Constant-Expression Initializer, The Value of That
Constant Expression, IMPLICITLY
Converted to the underlying type of the enjoy, is the associated value of
The enum member. if the decaration
Of The Enum MEMBER HAS No Initializer, ITS Associated Value IS Set
Implicitly, As Follows:
? If The enum merr is the first enum member declared in The Enum Type,
ITS Associated Value is Zero.
? Otherwise, The Associated Value of the Enum Member Is Obtained by
Increasing the associated value of
THE TEXTUALLY Preceding Enum Member by One. This Increased Value Must BEWITHIN THE RANGE OF VALUES
That can be represented by the underlying type.
Chapter 21 ENUMS
295
[EXAMPLE: THE EXAMPLE
Using system;
ENUM Color
{
RED,
Green = 10,
Blue
}
Class test
{
Static void main () {
Console.writeline (StringFromColor);
Console.writeline (StringFromColor);
Console.writeline (StringFromColor);
}
STATIC STRING STRINGFROMCOLOR (Color C) {
Switch (c) {
Case color.red:
Return string.format ("RED = {0}", (int) c);
Case Color.green:
Return string.format ("Green = {0}", (int) c);
Case color.blue:
Return string.Format ("Blue = {0}", (int) c);
DEFAULT:
Return "Invalid Color";
}
}
}
PRINTS OUT The Enum Member Names and Their Associated Values. The Output IS:
RED = 0
Green = 10
Blue = 11
For The Following Reasons:
? The enum member red is automaticly assigned the value zero (Since IT
HAS No Initializer and is The FirstIz
ENUM MEMBER);
? The enum member green is expenes xiX;
? And the enum member blue is Automatically Assigned The Value ONE GREATER
Than the Member That
TEXTUALLY Precedes IT.
End example]
THE Associated Value of An Enum Member May Not, Directly or Indirectly, USE
The value of its oow associated
ENUM MEMBER. Other Than this Circularity Restriction, ENUM MEMBER
Initializers May Freely Refer To Other
ENUM Member Initializers, Regardless of Their Textual Position. Withnin An
ENUM Member Initializer, Values of
Other Enum Members Are Always Treated As Having The Type of THEIR
Underlying Type, SO That Casts Are Not
NECESSARY WHEN REFERRING TO OTHER ENUM MEMBERS.
[EXAMPLE: The Exampleenum Circular
{
A = B,
B
}
Results in a Compile-Time Error Because The Declarations of A and B Are
Circular. A Depends on b Explicitly,
AND B Depends on a implicitly. End Example]
C # language specification
296
ENUM MEMBERS Aren named and scoped in a manner exactly analog to fields
Withnin classes. The scope of
An Enum Member Is The Body of Its Containing Enum Type. Within That Scope,
ENUM MEMBERS Can Be ReferRed
TO by their simple name. from all other code, the name of an enum member
Must be qualified with the name
Of Its Enum Type. ENUM MEMBERS DO NOT HAVE ANY DECLARED Accessibility? An
ENUM MEMBER IS Accessible IF
ITS Containing Enum Type is accessible.