枚举Enums

枚举类型是包含一组已命名常量的独特值类型。An enum type is a distinct value type with a set of named constants. 需要定义包含一组离散值的类型时,可以定义枚举。You define enums when you need to define a type that can have a set of discrete values. 枚举使用一种整型值类型作为其基础存储,They use one of the integral value types as their underlying storage. 并提供离散值的语义含义。They provide semantic meaning to the discrete values.

以下示例声明并使用名为“Color”的 enum 类型,其中包含三个常量值(RedGreenBlue)。The following example declares and uses an enum type named Color with three constant values, Red, Green, and Blue.

using System;
enum Color
{
    Red,
    Green,
    Blue
}
class EnumExample
{
    static void PrintColor(Color color) 
    {
        switch (color) 
        {
            case Color.Red:
                Console.WriteLine("Red");
                break;
            case Color.Green:
                Console.WriteLine("Green");
                break;
            case Color.Blue:
                Console.WriteLine("Blue");
                break;
            default:
                Console.WriteLine("Unknown color");
                break;
        }
    }
    static void Main() 
    {
        Color c = Color.Red;
        PrintColor(c);
        PrintColor(Color.Blue);
    }
}

每个 enum 类型都有对应的整型类型(称为 enum 类型的基础类型)。Each enum type has a corresponding integral type called the underlying type of the enum type. 如果 enum 类型未显式声明基础类型,则基础类型为 intAn enum type that does not explicitly declare an underlying type has an underlying type of int. enum 类型的存储格式和可取值范围由基础类型决定。An enum type’s storage format and range of possible values are determined by its underlying type. enum 类型需要使用的一组值不受其 enum 成员限制。The set of values that an enum type can take on is not limited by its enum members. 尤其是,基础类型的 enum 的任何值都可以显式转换成 enum 类型,并作为 enum 类型的不同有效值。In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type.

以下示例声明基础类型为 sbyte 且名为“Alignment”的 enum 类型。The following example declares an enum type named Alignment with an underlying type of sbyte.

enum Alignment: sbyte
{
    Left = -1,
    Center = 0,
    Right = 1
}

如上面的示例所示,enum 成员声明可以包含用于指定成员值的常数表达式。As shown by the previous example, an enum member declaration can include a constant expression that specifies the value of the member. 每个 enum 成员的常量值都必须介于 enum 的基础类型范围内。The constant value for each enum member must be in the range of the underlying type of the enum. 如果 enum 成员声明未显式指定值,那么会为成员指定值 0(如果是 enum 类型中的首个成员)或原文前一个 enum 成员的值加 1。When an enum member declaration does not explicitly specify a value, the member is given the value zero (if it is the first member in the enum type) or the value of the textually preceding enum member plus one.

可使用类型显式转换功能将 Enum 值转换成整型值,反之亦然。Enum values can be converted to integral values and vice versa using type casts. 例如:For example:

int i = (int)Color.Blue;    // int i = 2;
Color c = (Color)2;         // Color c = Color.Blue;  

任何 enum 类型的默认值都是已转换成 enum 类型的整型值 0。The default value of any enum type is the integral value zero converted to the enum type. 如果变量被自动初始化为默认值,这就是为 enum 类型的变量指定的值。In cases where variables are automatically initialized to a default value, this is the value given to variables of enum types. 为了让 enum 类型的默认值可供方便使用,文本类型 0 隐式转换成任意 enum 类型。In order for the default value of an enum type to be easily available, the literal 0 implicitly converts to any enum type. 因此,可以运行以下命令。Thus, the following is permitted.

Color c = 0;