枚举格式字符串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 table lists the enumeration formatting strings and the values they return. 这些格式说明符不区分大小写。These format specifiers are not case-sensitive.

格式字符串Format string 结果Result
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                               
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                               
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                               
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 value is a minimum eight digits in length. 下面的示例演示 X 格式说明符。The following example illustrates the X format specifier.

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                               
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

格式设置类型Formatting Types