枚举 (enum) 是值类型的一种特殊形式,它从 System.Enum 继承而来,并为基础基元类型的值提供替代名称。枚举类型有名称、基础类型和一组字段。基础类型必须是一个内置的有符号(或无符号)整数类型(如 Byte、Int32 或 UInt64)。字段是静态文本字段,其中的每一个字段都表示常数。同一个值可以分配给多个字段。出现这种情况时,必须将其中某个值标记为主要枚举值,以便进行反射和字符串转换。
可以将基础类型的值分配给枚举,反之亦然(运行库不要求强制转换)。可创建枚举的实例,并调用 System.Enum 的方法以及对枚举的基础类型定义的任何方法。但是,某些语言可能不允许在要求基础类型的实例时作为参数传递枚举(反之亦然)。
对于枚举还有以下附加限制:
它们不能定义自己的方法。
它们不能实现接口。
它们不能定义属性或事件。
除非枚举只是因为嵌套在泛型类型中因而成为泛型,否则枚举不能为泛型。也就是说,枚举不能有自己的类型参数。
注意 |
|---|
|
用 Visual Basic、C# 和 C++ 创建的嵌套类型(包括枚举)包含所有封闭泛型类型的类型参数,因此即使这些嵌套类型没有自己的类型参数,它们也是泛型的。有关更多信息,请参见 MakeGenericType 中的“嵌套类型”。 |
可以在 Microsoft 中间语言 (MSIL) 程序集语言中声明泛型枚举,但如果尝试使用该枚举,则会引发 TypeLoadException。
Flags 属性表示一种特殊的枚举,称为位域。运行库本身不区分传统枚举与位域,但您的语言可能会区分二者。当区分二者的时候,可以对位域(而不是枚举)使用位操作符以产生未命名的值。枚举一般用于列出唯一的元素,如一周的各天、国家/地区名称,等等。位域一般用于列出可能联合发生的质量或数量,比如 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("\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