列舉格式字串

您可以使用 Enum.ToString 方法來建立新的字串物件,以表示列舉成員的數值、十六進位值或字串值。 這個方法會採用其中一個列舉格式字串,來指定您想要傳回的值。

下列各節列出列舉格式字串及其傳回的值。 這些格式規範不區分大小寫。

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

以十六進位值來顯示列舉項目。 值在必要的情況下會以前置零來表示,以確保結果字串使用兩個字元代表列舉類型之底層數值類型每個位元組。 下列範例說明 X 格式規範。 在範例中,DayOfWeekConsoleColorFileAttributesInt32,或者是 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

範例

下列範例會定義名為 Colors 的列舉,該列舉是由三個項目所組成︰RedBlueGreen

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.      

另請參閱