列舉型別
列舉型別 (Enum) 是一種特殊格式的數值型別,從 System.Enum 繼承而來,並可為基礎的基本型別數值提供替代名稱。列舉型別具有名稱、基礎型別和一組欄位。基礎型別必須是內建帶正負號的整數 (Signed Integer) 或不帶正負號的整數 (Unsigned Integer) 型別之一 (例如 Byte、Int32 或 UInt64)。欄位為靜態的常值欄位,每一個欄位各代表一個常數。相同的數值可指定給多個欄位。發生這種情況時,必須將其中一個數值標記為反映和字串轉換的主要列舉值。
您可以將基礎型別的值指定給列舉型別,反之亦然 (Runtime 不需要轉型)。您可以建立列舉型別的執行個體,然後呼叫 System.Enum 的方法以及在列舉型別的基礎型別上定義的任何方法。但是,在需要基礎型別的執行個體時,有些語言可能不允許您將列舉型別當成參數傳遞 (反之亦然)。
下列限制適用於列舉型別:
- 不能定義自己的方法。
- 不能實作介面。
- 不能定義屬性或事件。
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
[C#]
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("\r\nThe 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("\t" +
((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
請參閱
通用型別系統 | 數值型別 | 內建數值型別 | System.ValueType |System.Enum