列舉類型 (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 ; 它們是從零開始,並依照定義文字順序遞增一。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. 也就是說,這些列舉成員的關聯值應該是兩個的乘冪。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
    }
}

如需詳細資訊和範例,請參閱 [api 參考] 頁面的 [ System.FlagsAttribute api 參考] 頁面和 [ [非獨佔成員] 和 旗標屬性 ] 區段 System.EnumFor 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.string 型別和列舉條件約束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.EnumFor 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