Share via


列舉型別格式字串

更新:2007 年 11 月

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

下表列出列舉型別格式字串和它們傳回的值。這些格式規範不區分大小寫。

格式字串

結果

G 或 g

將列舉型別項目顯示為字串值,如果可能的話,否則顯示目前執行個體的整數值。如果列舉型別因設定 Flags 屬性 (Attribute) 而被定義,各個有效項目的字串值都會以逗號分隔而串連在一起。如果 Flags 屬性未設定,無效值將顯示為數值項目。以下範例說明 G 格式規範。

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

F 或 f

將列舉型別項目顯示為字串值,如果可能的話。如果值可以完整顯示為列舉型別中項目的總和 (即使 Flags 屬性不出現),各個有效項目的字串值都會以逗號分隔而串連在一起。如果值無法完全由列舉型別項目來決定,那麼這值便會格式化為整數值。以下範例說明 F 格式規範。

Console.WriteLine(ConsoleColor.Blue.ToString("F")) ' Displays Blue
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
FileAttributes.Archive
Console.WriteLine(attributes.ToString("F")) ' Displays Hidden, Archive
Console.WriteLine(ConsoleColor.Blue.ToString("F")); // Displays Blue
FileAttributes attributes = FileAttributes.Hidden |
FileAttributes.Archive;
Console.WriteLine(attributes.ToString("F")); // Displays Hidden, Archive

D 或 d

以盡可能簡短的表示,將列舉型別項目顯示為整數值。以下範例說明 D 格式規範。

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

X 或 x

將列舉型別項目顯示為十六進位值。值以必要的前置零字元來表示,以確保這值最少有八位數長度。以下範例說明 X 格式規範。

Console.WriteLine(ConsoleColor.Cyan.ToString("X")) ' Displays 0000000B
Dim attributes As FileAttributes = FileAttributes.Hidden Or _
FileAttributes.Archive
Console.WriteLine(attributes.ToString("X")) ' Displays 00000022
Console.WriteLine(ConsoleColor.Cyan.ToString("X")); // Displays 0000000B
FileAttributes attributes = FileAttributes.Hidden |
FileAttributes.Archive;
Console.WriteLine(attributes.ToString("X")); // Displays 00000022

範例

下列範例定義稱為 Colors 的列舉型別,它由三個項目組成:Red、Blue 和 Green。

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

列舉型別定義之後,可用下列方式來宣告執行個體。

Dim myColor As Color = Color.Green      
Color myColor = 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.      

請參閱

其他資源

格式化型別