列挙型形式文字列

Enum.ToString メソッドを使用すると、列挙型メンバーの数値、16 進数、または文字列値を表す新しい文字列オブジェクトを作成できます。 このメソッドは、列挙型書式指定文字列のいずれかを使って、返される値を指定します。

次のセクションでは、列挙型書式指定文字列とそれが返す値を一覧表示します。 これらの書式指定子では大文字と小文字は区別されません。

G または g

可能な場合には列挙エントリを文字列値として表示し、それ以外の場合は、現在のインスタンスの整数値を表示します。 列挙型が FlagsAttribute を設定して定義されている場合、有効な各エントリの文字列値はコンマで区切られて連結されます。 Flags 属性が設定されていない場合、無効な値が数値エントリとして表示されます。 次の例は、G 書式指定子を示しています。

Console.WriteLine(((DayOfWeek)7).ToString("G"));      // 7
Console.WriteLine(ConsoleColor.Red.ToString("G"));    // Red

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("G"));          // Hidden, Archive
Console.WriteLine((CType(7, DayOfWeek)).ToString("G"))    ' 7
Console.WriteLine(ConsoleColor.Red.ToString("G"))         ' Red
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("G"))               ' Hidden, Archive

F または f

可能な場合には列挙エントリを文字列値として表示します。 (Flags 属性が存在しない場合でも) 値が列挙内のエントリの総和として表示できる場合、有効な各エントリの文字列値はコンマで区切られて連結されます。 値が列挙エントリによって特定できない場合、その値は整数値として書式設定されます。 次の例は、F 書式指定子を示しています。

Console.WriteLine(((DayOfWeek)7).ToString("F"));       // Monday, Saturday
Console.WriteLine(ConsoleColor.Blue.ToString("F"));    // Blue

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("F"));           // Hidden, Archive
Console.WriteLine((CType(7, DayOfWeek)).ToString("F"))    ' Monday, Saturday
Console.WriteLine(ConsoleColor.Blue.ToString("F"))        ' Blue
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("F"))               ' Hidden, Archive

D または d

列挙エントリを整数値として可能な限り短い表現で表示します。 次の例は、D 書式指定子を示しています。

Console.WriteLine(((DayOfWeek)7).ToString("D"));       // 7
Console.WriteLine(ConsoleColor.Cyan.ToString("D"));    // 11

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("D"));           // 34
Console.WriteLine((CType(7, DayOfWeek)).ToString("D"))     ' 7
Console.WriteLine(ConsoleColor.Cyan.ToString("D"))         ' 11
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("D"))                ' 34

X または x

列挙エントリを 16 進値として表示します。 列挙型の基になる数値型で結果文字列にバイトあたり文字が 2 つ与えられるように、必要に応じてこの値の先頭にゼロが付きます。 次の例は、X 書式指定子を示しています。 例では、DayOfWeekConsoleColor、および FileAttributes の基になる型は Int32 か 32 ビット (4 バイト) の整数になり、8 文字からなる結果文字列が生成されます。

Console.WriteLine(((DayOfWeek)7).ToString("X"));       // 00000007
Console.WriteLine(ConsoleColor.Cyan.ToString("X"));    // 0000000B

var attributes = FileAttributes.Hidden | FileAttributes.Archive;
Console.WriteLine(attributes.ToString("X"));           // 00000022
Console.WriteLine((CType(7, DayOfWeek)).ToString("X"))    ' 00000007
Console.WriteLine(ConsoleColor.Cyan.ToString("X"))        ' 0000000B
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
                                   FileAttributes.Archive
Console.WriteLine(attributes.ToString("X"))               ' 00000022

次の例では、RedBlueGreen の 3 つのエントリから構成される、Colors と呼ばれる列挙型を定義します。

public enum Color { Red = 1, Blue = 2, Green = 3 };
Public Enum Color
    Red = 1
    Blue = 2
    Green = 3
End Enum

列挙型を定義すると、次のようにインスタンスを宣言できます。

Color myColor = Color.Green;
Dim myColor As Color = Color.Green

これで Color.ToString(System.String) メソッドを使用して、渡された書式指定子に応じて、列挙値をさまざまな方法で表示することができます。

Console.WriteLine("The value of myColor is {0}.",
                  myColor.ToString("G"));
Console.WriteLine("The value of myColor is {0}.",
                  myColor.ToString("F"));
Console.WriteLine("The value of myColor is {0}.",
                  myColor.ToString("D"));
Console.WriteLine("The value of myColor is 0x{0}.",
                  myColor.ToString("X"));
// The example displays the following output to the console:
//       The value of myColor is Green.
//       The value of myColor is Green.
//       The value of myColor is 3.
//       The value of myColor is 0x00000003.
Console.WriteLine("The value of myColor is {0}.", _
                  myColor.ToString("G"))
Console.WriteLine("The value of myColor is {0}.", _
                  myColor.ToString("F"))
Console.WriteLine("The value of myColor is {0}.", _
                  myColor.ToString("D"))
Console.WriteLine("The value of myColor is 0x{0}.", _
                  myColor.ToString("X"))
' The example displays the following output to the console:
'       The value of myColor is Green.
'       The value of myColor is Green.
'       The value of myColor is 3.
'       The value of myColor is 0x00000003.      

関連項目