共用方式為


一般型別系統中的列舉型別

更新:2007 年 11 月

列舉型別 (Enum) 是一種特殊格式的實值型別,從 System.Enum 繼承而來,並可為基礎的基本型別數值提供替代名稱。列舉型別具有名稱、基礎型別和一組欄位。基礎型別必須是內建帶正負號的整數 (Signed Integer) 或不帶正負號的整數 (Unsigned Integer) 型別之一 (例如 ByteInt32UInt64)。欄位為靜態的常值欄位,每一個欄位各代表一個常數。相同的數值可指定給多個欄位。發生這種情況時,必須將其中一個數值標記為反映和字串轉換的主要列舉值。

您可以將基礎型別的值指定給列舉型別,反之亦然 (Runtime 不需要轉型)。您可以建立列舉型別的執行個體,然後呼叫 System.Enum 的方法以及在列舉型別的基礎型別上定義的任何方法。但是,在需要基礎型別的執行個體時,有些語言可能不允許您將列舉型別當成參數傳遞 (反之亦然)。

下列限制適用於列舉型別:

  • 不能定義自己的方法。

  • 不能實作介面。

  • 不能定義屬性或事件。

  • 列舉型別不能是泛型,除非是巢狀於泛型型別中,才能是泛型。換句話說,列舉型別不能有自己的型別參數。

    注意事項:

    使用 Visual Basic、C# 和 C++ 建立的巢狀型別 (包括列舉型別) 包含所有封入泛型型別的型別參數,因此,即使沒有自己的型別參數,仍舊是泛型型別。如需詳細資訊,請參閱 MakeGenericType 中的<巢狀型別>。

    您可以在 Microsoft Intermediate Language (MSIL) 組件語言中宣告泛型列舉型別,但是如果試圖使用列舉型別,將會發生 TypeLoadException

Flags 屬性代表一種特殊的列舉型別,稱為位元欄位 (Bit Field)。Runtime 本身無法區別傳統列舉型別和位元欄位,但是您所使用的語言或許可以區分出來。在區分時,可在位元欄位上使用位元 (Bitwise) 運算子來產生未命名的數值,但是不能用於列舉型別。列舉型別通常用於唯一的項目 (Element) 清單,例如該星期的天數、國別或區域名稱等。位元欄位通常用於可能以組合形式出現的特性或數量清單,例如 Red And Big And Fast。

下列範例顯示如何使用位元欄位和傳統列舉型別。

Imports System
Imports System.Collections

' A traditional enumeration of some root vegetables.
Public Enum SomeRootVegetables
    HorseRadish
    Radish
    Turnip
End Enum 'SomeRootVegetables

' A bit field or flag enumeration of harvesting seasons.
<Flags()> Public Enum Seasons
   None = 0
   Summer = 1
   Autumn = 2
   Winter = 4
   Spring = 8
   All = Summer Or Autumn Or Winter Or Spring
End Enum 'Seasons

' Entry point.
Public Class EnumerationSample
    
    Public Shared Sub Main()
        ' Hash table of when vegetables are available.
        Dim AvailableIn As New Hashtable()
        
        AvailableIn(SomeRootVegetables.HorseRadish) = Seasons.All
        AvailableIn(SomeRootVegetables.Radish) = Seasons.Spring
        AvailableIn(SomeRootVegetables.Turnip) = Seasons.Spring Or _
            Seasons.Autumn
        
        ' Array of the seasons, using the enumeration.
        Dim MySeasons() As Seasons = {Seasons.Summer, Seasons.Autumn, _
            Seasons.Winter, Seasons.Spring}
        
        ' Print information of what vegetables are available each season.
        Dim i As Integer
        For i = 0 To MySeasons.Length - 1
            Console.WriteLine( _
                "The following root vegetables are harvested in " _
                & MySeasons(i).ToString("G") & ":")
            Dim e As DictionaryEntry
            For Each e In AvailableIn
                ' A bitwise comparison.
                If(CType(e.Value, Seasons) And MySeasons(i)) > 0 Then
                    Console.WriteLine("  " & _
                        CType(e.Key, SomeRootVegetables).ToString("G"))
                End If
            Next e
        Next i
    End Sub 'Main
End Class 'EnumerationSample
using System;
using System.Collections;

// A traditional enumeration of some root vegetables.
public enum SomeRootVegetables
{
    HorseRadish,
    Radish,
    Turnip
}

// A bit field or flag enumeration of harvesting seasons.
[Flags]
public enum Seasons
{
    None = 0,
    Summer = 1,
    Autumn = 2,
    Winter = 4,
    Spring = 8,
    All = Summer | Autumn | Winter | Spring
}

// Entry point.
public class EnumerationSample
{
    public static void Main()
    {
        // Hash table of when vegetables are available.
        Hashtable AvailableIn = new Hashtable();

        AvailableIn[SomeRootVegetables.HorseRadish] = Seasons.All;
        AvailableIn[SomeRootVegetables.Radish] = Seasons.Spring;
        AvailableIn[SomeRootVegetables.Turnip] = Seasons.Spring | 
            Seasons.Autumn;

        // Array of the seasons, using the enumeration.
        Seasons[] seasons = new Seasons[] { Seasons.Winter, Seasons.Spring, 
            Seasons.Summer, Seasons.Autumn };

        // Print information of what vegetables are available each season.
        for (int i = 0; i < seasons.Length; i++)
        {
            Console.WriteLine(
                "The following root vegetables are harvested in "
                + seasons[i].ToString("G") + ":");
            foreach (DictionaryEntry e in AvailableIn)
            {
                // A bitwise comparison.
                if (((Seasons)e.Value & seasons[i]) > 0)
                    Console.WriteLine("  " +
                        ((SomeRootVegetables)e.Key).ToString("G"));
            }
        }
    }
}

這個程式的輸出如下。

The following root vegetables are harvested in Summer:
  HorseRadish
The following root vegetables are harvested in Autumn:
  Turnip
  HorseRadish
The following root vegetables are harvested in Winter:
  HorseRadish
The following root vegetables are harvested in Spring:
  Turnip
  Radish
  HorseRadish

請參閱

概念

一般型別系統中的實值型別

.NET Framework 類別庫概觀

參考

System.ValueType

System.Enum

其他資源

一般型別系統