枚举格式字符串
可以使用 Enum.ToString 方法,新建表示枚举成员的数字值、十六进制值或字符串值的字符串对象。 此方法采用枚举格式设置字符串之一来指定要返回的值。
以下各部分列出了枚举格式设置字符串和它们返回的值。 这些格式说明符不区分大小写。
G 或 g
如有可能,将枚举项显示为字符串值,否则显示当前实例的整数值。 如果枚举使用 Flags 属性集进行定义,则每个有效项的字符串值会连接在一起(以逗号分隔)。 如果未设置 Flags 属性,则将无效值显示为数字项。 下面的示例演示 G 格式说明符。
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 或 f
如有可能,将枚举项显示为字符串值。 如果值可以完全显示为枚举中项的总和(即使未提供 Flags 属性),则每个有效项的字符串值会连接在一起(以逗号分隔)。 如果值不能由枚举项完全确定,则值会格式化为整数值。 下面的示例演示 F 格式说明符。
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 或 d
以尽可能短的表示形式将枚举项显示为整数值。 下面的示例演示 D 格式说明符。
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 或 x
将枚举项显示为十六进制值。 根据需要以前导零表示此值,以确保在枚举类型的基础数值类型中,结果字符串的每个字节都有两个字符。 下面的示例演示 X 格式说明符。 在示例中,这两者的基础类型 ConsoleColor 和 FileAttributes 为 Int32,或 32 位(或 4 字节)整数,它将生成 8 个字符的结果字符串。
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
示例
下面的示例定义一个名为 Colors
的枚举,它由三个项组成:Red
、Blue
和 Green
。
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.