列舉格式字串Enumeration format strings

您可以使用 Enum.ToString 方法來建立新的字串物件,以表示列舉成員的數值、十六進位值或字串值。You can use the Enum.ToString method to create a new string object that represents the numeric, hexadecimal, or string value of an enumeration member. 這個方法會採用其中一個列舉格式字串,來指定您想要傳回的值。This method takes one of the enumeration formatting strings to specify the value that you want returned.

下列各節列出列舉格式字串及其傳回的值。The following sections list the enumeration formatting strings and the values they return. 這些格式規範不區分大小寫。These format specifiers are not case-sensitive.

G 或 gG or g

盡可能以字串值來顯示列舉項目;如果不可能,則會顯示目前執行個體的整數值。Displays the enumeration entry as a string value, if possible, and otherwise displays the integer value of the current instance. 如果列舉是使用 Flags 屬性集來定義,每個有效項目的字串值會串連在一起,並以逗號分隔。If the enumeration is defined with the Flags attribute set, the string values of each valid entry are concatenated together, separated by commas. 如果未設定 Flags 屬性,則會以數值項目顯示無效的值。If the Flags attribute is not set, an invalid value is displayed as a numeric entry. 下列範例說明 G 格式規範。The following example illustrates the G format specifier.

Console.WriteLine(ConsoleColor.Red.ToString("G"));         // Displays Red
FileAttributes attributes = FileAttributes.Hidden |
                            FileAttributes.Archive;
Console.WriteLine(attributes.ToString("G"));   // Displays Hidden, Archive                               
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                               

F 或 fF or f

盡可能以字串值來顯示列舉項目。Displays the enumeration entry as a string value, if possible. 如果此值可完整顯示為列舉中的項目總合 (即使 Flags 屬性不存在),每個有效項目的字串值會串連在一起,並以逗號分隔。If the value can be completely displayed as a summation of the entries in the enumeration (even if the Flags attribute is not present), the string values of each valid entry are concatenated together, separated by commas. 如果該值不完全取決於列舉項目,則會格式化為整數值。If the value cannot be completely determined by the enumeration entries, then the value is formatted as the integer value. 下列範例說明 F 格式規範。The following example illustrates the F format specifier.

Console.WriteLine(ConsoleColor.Blue.ToString("F"));       // Displays Blue
FileAttributes attributes = FileAttributes.Hidden | 
                            FileAttributes.Archive;
Console.WriteLine(attributes.ToString("F"));   // Displays Hidden, Archive                               
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                               

D 或 dD or d

盡可能以最短的整數值表示來顯示列舉項目。Displays the enumeration entry as an integer value in the shortest representation possible. 下列範例說明 D 格式規範。The following example illustrates the D format specifier.

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

X 或 xX or x

以十六進位值來顯示列舉項目。Displays the enumeration entry as a hexadecimal value. 值在必要的情況下會以前置零來表示,以確保結果字串使用兩個字元代表列舉類型之底層數值類型每個位元組。The value is represented with leading zeros as necessary, to ensure that the result string has two characters for each byte in the enumeration type's underlying numeric type. 下列範例說明 X 格式規範。The following example illustrates the X format specifier. 在範例中,ConsoleColorFileAttributes 兩者的底層類型是 Int32,或 32 位元 (或 4 位元) 整數,這會產生 8 個字元的結果字串。In the example, the underlying type of both ConsoleColor and FileAttributes is Int32, or a 32-bit (or 4-byte) integer, which produces an 8-character result string.

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

範例Example

下列範例會定義名為 Colors 的列舉,該列舉是由三個項目所組成︰RedBlueGreenThe following example defines an enumeration called Colors that consists of three entries: Red, Blue, and Green.

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

定義列舉之後,可利用下列方式宣告執行個體。After the enumeration is defined, an instance can be declared in the following manner.

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

然後可使用 Color.ToString(System.String) 方法,根據傳遞給列舉值的格式規範,以不同的方式來顯示列舉值。The Color.ToString(System.String) method can then be used to display the enumeration value in different ways, depending on the format specifier passed to it.

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.      

請參閱See also