枚举类型(C# 参考)Enumeration types (C# reference)

枚举类型 是由基础整型数值类型的一组命名常量定义的值类型An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. 若要定义枚举类型,请使用 enum 关键字并指定枚举成员 的名称:To define an enumeration type, use the enum keyword and specify the names of enum members:

enum Season
{
    Spring,
    Summer,
    Autumn,
    Winter
}

默认情况下,枚举成员的关联常数值为类型 int;它们从零开始,并按定义文本顺序递增 1。By default, the associated constant values of enum members are of type int; they start with zero and increase by one following the definition text order. 可以显式指定任何其他整数数值类型作为枚举类型的基础类型。You can explicitly specify any other integral numeric type as an underlying type of an enumeration type. 还可以显式指定关联的常数值,如下面的示例所示:You can also explicitly specify the associated constant values, as the following example shows:

enum ErrorCode : ushort
{
    None = 0,
    Unknown = 1,
    ConnectionLost = 100,
    OutlierReading = 200
}

不能在枚举类型的定义内定义方法。You cannot define a method inside the definition of an enumeration type. 若要向枚举类型添加功能,请创建扩展方法To add functionality to an enumeration type, create an extension method.

枚举类型 E 的默认值是由表达式 (E)0 生成的值,即使零没有相应的枚举成员也是如此。The default value of an enumeration type E is the value produced by expression (E)0, even if zero doesn't have the corresponding enum member.

可以使用枚举类型,通过一组互斥值或选项组合来表示选项。You use an enumeration type to represent a choice from a set of mutually exclusive values or a combination of choices. 若要表示选项组合,请将枚举类型定义为位标志。To represent a combination of choices, define an enumeration type as bit flags.

作为位标志的枚举类型Enumeration types as bit flags

如果希望枚举类型表示选项组合,请为这些选项定义枚举成员,以便单个选项成为位字段。If you want an enumeration type to represent a combination of choices, define enum members for those choices such that an individual choice is a bit field. 也就是说,这些枚举成员的关联值应该是 2 的幂。That is, the associated values of those enum members should be the powers of two. 然后,可以使用按位逻辑运算符|& 分别合并选项或交叉组合选项。Then, you can use the bitwise logical operators | or & to combine choices or intersect combinations of choices, respectively. 若要指示枚举类型声明位字段,请对其应用 Flags 属性。To indicate that an enumeration type declares bit fields, apply the Flags attribute to it. 如下面的示例所示,还可以在枚举类型的定义中包含一些典型组合。As the following example shows, you can also include some typical combinations in the definition of an enumeration type.

[Flags]
public enum Days
{
    None      = 0b_0000_0000,  // 0
    Monday    = 0b_0000_0001,  // 1
    Tuesday   = 0b_0000_0010,  // 2
    Wednesday = 0b_0000_0100,  // 4
    Thursday  = 0b_0000_1000,  // 8
    Friday    = 0b_0001_0000,  // 16
    Saturday  = 0b_0010_0000,  // 32
    Sunday    = 0b_0100_0000,  // 64
    Weekend   = Saturday | Sunday
}

public class FlagsEnumExample
{
    public static void Main()
    {
        Days meetingDays = Days.Monday | Days.Wednesday | Days.Friday;
        Console.WriteLine(meetingDays);
        // Output:
        // Monday, Wednesday, Friday

        Days workingFromHomeDays = Days.Thursday | Days.Friday;
        Console.WriteLine($"Join a meeting by phone on {meetingDays & workingFromHomeDays}");
        // Output:
        // Join a meeting by phone on Friday

        bool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday;
        Console.WriteLine($"Is there a meeting on Tuesday: {isMeetingOnTuesday}");
        // Output:
        // Is there a meeting on Tuesday: False

        var a = (Days)37;
        Console.WriteLine(a);
        // Output:
        // Monday, Wednesday, Saturday
    }
}

有关详细信息和示例,请参阅 System.FlagsAttribute API 参考页和 System.Enum API 参考页的非独占成员和 Flags 属性部分。For more information and examples, see the System.FlagsAttribute API reference page and the Non-exclusive members and the Flags attribute section of the System.Enum API reference page.

System.Enum 类型和枚举约束The System.Enum type and enum constraint

System.Enum 类型是所有枚举类型的抽象基类。The System.Enum type is the abstract base class of all enumeration types. 它提供多种方法来获取有关枚举类型及其值的信息。It provides a number of methods to get information about an enumeration type and its values. 有关更多信息和示例,请参阅 System.Enum API 参考页。For more information and examples, see the System.Enum API reference page.

从 C# 7.3 开始,你可以在基类约束中使用 System.Enum(称为枚举约束),以指定类型参数为枚举类型。Beginning with C# 7.3, you can use System.Enum in a base class constraint (that is known as the enum constraint) to specify that a type parameter is an enumeration type.

转换Conversions

对于任何枚举类型,枚举类型与其基础整型类型之间存在显式转换。For any enumeration type, there exist explicit conversions between the enumeration type and its underlying integral type. 如果将枚举值转换为其基础类型,则结果为枚举成员的关联整数值。If you cast an enum value to its underlying type, the result is the associated integral value of an enum member.

public enum Season
{
    Spring,
    Summer,
    Autumn,
    Winter
}

public class EnumConversionExample
{
    public static void Main()
    {
        Season a = Season.Autumn;
        Console.WriteLine($"Integral value of {a} is {(int)a}");  // output: Integral value of Autumn is 2

        var b = (Season)1;
        Console.WriteLine(b);  // output: Summer

        var c = (Season)4;
        Console.WriteLine(c);  // output: 4
    }
}

使用 Enum.IsDefined 方法来确定枚举类型是否包含具有特定关联值的枚举成员。Use the Enum.IsDefined method to determine whether an enumeration type contains an enum member with the certain associated value.

对于任何枚举类型,都存在分别与 System.Enum 类型的装箱和取消装箱相互转换。For any enumeration type, there exist boxing and unboxing conversions to and from the System.Enum type, respectively.

C# 语言规范C# language specification

有关更多信息,请参阅 C# 语言规范的以下部分:For more information, see the following sections of the C# language specification:

请参阅See also