Redigeeri

Jagamisviis:


Enumeration format strings

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 aren't case-sensitive.

G or g

Displays the enumeration entry as a string value, if possible, and otherwise displays the integer value of the current instance. If the enumeration is defined with the FlagsAttribute set, the string values of each valid entry are concatenated together, separated by commas. If the Flags attribute isn't set, an invalid value is displayed as a numeric entry. The following example illustrates the G format specifier.

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

Displays the enumeration entry as a string value, if possible. If the value can be displayed as a summation of the entries in the enumeration (even if the Flags attribute isn't present), the string values of each valid entry are concatenated together, separated by commas. If the value can't be determined by the enumeration entries, then the value is formatted as the integer value. The following example illustrates the F format specifier.

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

Displays the enumeration entry as an integer value in the shortest representation possible. The following example illustrates the D format specifier.

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 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. The following example illustrates the X format specifier. In the example, the underlying types of DayOfWeek, ConsoleColor and FileAttributes is Int32, or a 32-bit (or 4-byte) integer, which produces an 8-character result string.

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

Example

The 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

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