Enum 構造体
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public MustInherit Class Enum Inherits ValueType Implements IComparable, IFormattable, IConvertible
[SerializableAttribute] [ComVisibleAttribute(true)] public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class Enum abstract : public ValueType, IComparable, IFormattable, IConvertible
列挙体は、基になる型が Char 以外の整数型である名前付き定数です。基になる型が明示的に宣言されない場合は、Int32 が使用されます。通常、プラミング言語には、名前付き定数とその値の組み合わせから成る列挙体を宣言する構文が用意されています。
Enum には、このクラスの複数のインスタンスを比較したり、インスタンスの値を文字列形式に変換したり、数値の文字列形式をこのクラスのインスタンスに変換したり、指定した列挙体および値のインスタンスを作成するためのメソッドが用意されています。
列挙体をビット フィールドとして扱うこともできます。詳細については、「FlagsAttribute」を参照してください。
実装されているインターフェイスこのクラスは、ValueType から継承し、IComparable、IFormattable、IConvertible の各インターフェイスを実装します。このクラスの明示的な IConvertible インターフェイス メンバの実装の代わりに、Convert クラスを変換のために使用します。
-
ビットごとの演算 (AND、OR、EXCLUSIVE OR) が数値で実行される場合のみ、列挙体の FlagsAttribute カスタム属性を使用します。
-
列挙定数を 2 の累乗 (1、2、4、8 など) で定義してください。これは、結合された列挙定数の各フラグが重複しないことを意味します。
-
よく使われるフラグの組み合わせに対して列挙定数を作成することを検討してください。たとえば、列挙定数の Read = 1 および Write = 2 を含むファイル I/O 操作に使用される列挙体を持っている場合は、Read フラグと Write フラグを結合する列挙定数 ReadWrite = Read OR Write を作成することが考えられます。場合によっては、フラグを結合するために使用されるビットごとの OR 演算は、単純なタスクには必要のない高度な概念と見なされます。
-
フラグ列挙定数として負数を定義する場合は、多数のフラグの位置が 1 に設定される場合があるので注意してください。その結果、コードが混乱してコーディング エラーの原因となる可能性があります。
-
フラグが数値で設定されているかどうかをテストする便利な方法は、数値とフラグ列挙定数の間でビットごとの AND 演算を実行することです。これにより、フラグに対応しない数値内のすべてのビットが 0 に設定されます。次に、その演算の結果がフラグ列挙定数に等しいかどうかをテストします。
-
値が 0 であるフラグ列挙定数の名前として None を使用します。フラグをテストするためにビットごとの AND 演算を実行する場合、None 列挙定数は結果が常に 0 になるので使用できません。ただし、数値と None 列挙定数の間で、ビットごとではなく論理的な比較を実行して、数値のビットが設定されているかどうかを判断できます。
フラグ列挙体の代わりに値の列挙体を作成する場合も、None 列挙定数の作成は有用です。列挙体に使用されるメモリが共通言語ランタイムによって既定で 0 に初期化されるからです。その結果、値が 0 である定数を定義しなかった場合、列挙体には作成時に無効な値が含まれます。
アプリケーションで、既定の状態を表す必要がある場合は、値が 0 である列挙定数を使用してその既定の状態を表すことを検討してください。既定の状態がない場合は、他の列挙定数では表現されない状態を表すために、値が 0 の列挙定数の使用を検討してください。
-
列挙体そのものの状態を反映するだけの列挙値を定義しないでください。たとえば、列挙体の末尾をマークするだけの列挙定数は定義しないでください。列挙体の末尾の値を確認する必要がある場合は、その値を明示的にチェックしてください。また、範囲内のすべての値が有効である場合は、先頭と末尾の列挙定数を確認する範囲のチェックを実行できます。
-
列挙定数を値として受け取るメソッドまたはプロパティを定義する場合は、値を検証することを検討してください。数値が列挙体に定義されていない場合も、数値を列挙型にキャストできるからです。
名前付きの値を表す列挙体と、名前付きのビット フィールドを表す別の列挙体を使用するコード例を次に示します。
Imports System Public Class EnumTest Enum Days Saturday Sunday Monday Tuesday Wednesday Thursday Friday End Enum 'Days Enum BoilingPoints Celcius = 100 Fahrenheit = 212 End Enum 'BoilingPoints <FlagsAttribute()> _ Enum Colors Red = 1 Green = 2 Blue = 4 Yellow = 8 End Enum 'Colors Public Shared Sub Main() Dim weekdays As Type = GetType(Days) Dim boiling As Type = GetType(BoilingPoints) Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:") Dim s As String For Each s In [Enum].GetNames(weekdays) Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(weekdays, [Enum].Parse(weekdays, s), "d")) Next s Console.WriteLine() Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.") Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:") For Each s In [Enum].GetNames(boiling) Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(boiling, [Enum].Parse(boiling, s), "d")) Next s Dim myColors As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow Console.WriteLine() Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors) End Sub 'Main End Class 'EnumTest
using System; public class EnumTest { enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }; enum BoilingPoints { Celcius = 100, Fahrenheit = 212 }; [FlagsAttribute] enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; public static void Main() { Type weekdays = typeof(Days); Type boiling = typeof(BoilingPoints); Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:"); foreach ( string s in Enum.GetNames(weekdays) ) Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enum.Parse(weekdays, s), "d")); Console.WriteLine(); Console.WriteLine("Enums can also be created which have values that represent some meaningful amount."); Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:"); foreach ( string s in Enum.GetNames(boiling) ) Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.Parse(boiling, s), "d")); Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow; Console.WriteLine(); Console.WriteLine("myColors holds a combination of colors. Namely: {0}" , myColors); } }
using namespace System; enum class Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }; enum class BoilingPoints { Celcius = 100, Fahrenheit = 212 }; [FlagsAttribute] enum class Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; int main() { Type^ weekdays = Days::typeid; Type^ boiling = BoilingPoints::typeid; Console::WriteLine( "The days of the week, and their corresponding values in the Days Enum are:" ); Array^ a = Enum::GetNames( weekdays ); Int32 i = 0; do { Object^ o = a->GetValue( i ); Console::WriteLine( "{0,-11}= {1}", o->ToString(), Enum::Format( weekdays, Enum::Parse( weekdays, o->ToString() ), "d" ) ); } while ( ++i < a->Length ); Console::WriteLine(); Console::WriteLine( "Enums can also be created which have values that represent some meaningful amount." ); Console::WriteLine( "The BoilingPoints Enum defines the following items, and corresponding values:" ); i = 0; Array^ b = Enum::GetNames( boiling ); do { Object^ o = b->GetValue( i ); Console::WriteLine( "{0,-11}= {1}", o->ToString(), Enum::Format( boiling, Enum::Parse( boiling, o->ToString() ), "d" ) ); } while ( ++i < b->Length ); Array^ c = Enum::GetNames( Colors::typeid ); Colors myColors = Colors::Red | Colors::Blue | Colors::Yellow; Console::WriteLine(); Console::Write( "myColors holds a combination of colors. Namely:" ); for ( i = 0; i < 3; i++ ) Console::Write( " {0}", c->GetValue( i ) ); }
import System.*; public class EnumTest { enum Days { saturday (0), sunday (1), monday (2), tuesday (3), wednesday (4), thursday (5), friday (6); } //Days enum BoilingPoints { celsius (100), fahrenheit (212); } //BoilingPoints /** @attribute FlagsAttribute() */ enum Colors { red (1), green (2), blue (4), yellow (8); } //Colors public static void main(String[] args) { Type weekdays = Days.class.ToType(); Type boiling = BoilingPoints.class.ToType(); Console.WriteLine("The days of the week, and their corresponding" + " values in the Days Enum are:"); String s[] = Enum.GetNames(weekdays); for (int iCtr = 0; iCtr < s.length; iCtr++) { Console.WriteLine("{0,-11}= {1}", s[iCtr], Enum.Format(weekdays, Enum.Parse(weekdays, s[iCtr]), "d")); } Console.WriteLine(); Console.WriteLine("Enums can also be created which have values that" + " represent some meaningful amount."); Console.WriteLine("The BoilingPoints Enum defines the following items ," + " and corresponding values:"); String s1[] = Enum.GetNames(boiling); for (int iCtr = 0; iCtr < s1.length; iCtr++) { Console.WriteLine("{0,-11}= {1}", s1[iCtr], Enum.Format(boiling, Enum.Parse(boiling, s1[iCtr]), "d")); } Colors myColors = Colors.red | Colors.blue | Colors.yellow; Console.WriteLine(); Console.WriteLine("myColors holds a combination of colors. Namely: {0}" , myColors); } //main } //EnumTest
import System; public class EnumTest { enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }; enum BoilingPoints { Celcius = 100, Fahrenheit = 212 }; FlagsAttribute enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; public static function Main() { var weekdays : Type = Days; var boiling : Type = BoilingPoints; Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:"); for( var i : int in Enum.GetNames(weekdays) ) Console.WriteLine( "{0,-11}= {1}", Enum.GetNames(weekdays).GetValue(i), Enum.Format( weekdays, Enum.Parse(weekdays, Enum.GetNames(weekdays).GetValue(i)), "d")); Console.WriteLine(); Console.WriteLine("Enums can also be created which have values that represent some meaningful amount."); Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:"); for ( var j : int in Enum.GetNames(boiling) ) Console.WriteLine( "{0,-11}= {1}", Enum.GetNames(boiling).GetValue(j), Enum.Format(boiling, Enum.Parse(boiling, Enum.GetNames(boiling).GetValue(j)), "d")); var myColors : Colors = Colors.Red | Colors.Blue | Colors.Yellow; Console.WriteLine(); Console.WriteLine("myColors holds a combination of colors. Namely: {0}" , myColors); } }
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- Enum 構造体のページへのリンク