共通型システムの列挙

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

列挙型 (enum) では、値型、System.Enum から継承され、基になるプリミティブ型の値の代替名を提供の特殊な形式です。 列挙型名を基になる種類、およびフィールドのセットを搭載します。 基になる型には、組み込みの符号付きまたは符号なし整数の種類 (バイト型 (Byte)Int32 のUInt64 など) のいずれかをする必要があります。 フィールドは静的リテラル フィールドをそれぞれあり、クラスの定数を表します。 同じ値を割り当てる複数のフィールドをことができます。 この現象が発生するのいずれかの値リフレクションと文字列の変換のためのプライマリ列挙値としてマークする必要があります。

基になる種類の列挙型に、またはその逆 (キャストは必要ありません、ランタイムによって) 値を割り当てることができます。 列挙型のインスタンスを作成するし System.Enum のメソッドと同様、列挙体の基になる種類で定義されている任意のメソッドを呼び出すことができます。 ただし、一部の言語ができませんパラメータとして列挙体を渡す、基になる型のインスタンスが必要な場合 (またはその逆)。

次の追加の制限がの列挙に適用します。

  • 独自のメソッドを定義できません。

  • インターフェイスを実装することはできません。

  • プロパティまたはイベントを定義することはできません。

  • 列挙型は、ジェネリック型に入れ子がためだけの一般的な限り、ジェネリック、できません。 つまり、列挙型は独自の型パラメータはできません。

    注意

    入れ子になった型 (列挙型を含む) Visual Basic、C#、および C++ で作成されたすべての外側ジェネリック型の型パラメータを含めるし、独自の型パラメータを持っていない場合でもが汎用ため。詳細については、「入れ子になった型を参照してください。MakeGenericType

    Microsoft 中間言語 (MSIL) アセンブリ言語では、ジェネリック列挙体を宣言することですが、列挙体を使用しよう場合に、TypeLoadException が発生します。

Flags 属性は特別なビット フィールドと呼ばれる列挙を表します。 ランタイム自体は区別されません従来の列挙型とビット フィールドが使用する言語可能性があります。 この区別が行われるとビットごとの AND 演算子できますはビット フィールドで列挙型にいない名前のない値を生成します。 列挙は、曜日、週、国または地域名などの一意の要素の一覧は通常使用され、ように。 ビット フィールドは、特性または 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

参照

参照

System.ValueType

System.Enum

概念

in the Common Type System 値型

.NET Framework クラス ライブラリの概要

その他の技術情報

共通型システム