Permalink
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (41 sloc) 4.4 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Enumeration Format Strings
03/30/2017
.net
dotnet-standard
article
format specifiers, enumeration format strings
enumeration format strings
formatting [.NET Framework], enumeration
dd1ff672-1052-42cf-8666-4924fb6cd1a1
13
rpetrusha
ronpet
wpickett

Enumeration Format Strings

You can use the xref:System.Enum.ToString%2A?displayProperty=nameWithType 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 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 Flags attribute set, the string values of each valid entry are concatenated together, separated by commas. If the Flags attribute is not set, an invalid value is displayed as a numeric entry. The following example illustrates the G format specifier.

[!code-csharpFormatting.Enum#1] [!code-vbFormatting.Enum#1]
F or f Displays the enumeration entry as a string value, if possible. 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. The following example illustrates the F format specifier.

[!code-csharpFormatting.Enum#2] [!code-vbFormatting.Enum#2]
D or d Displays the enumeration entry as an integer value in the shortest representation possible. The following example illustrates the D format specifier.

[!code-csharpFormatting.Enum#3] [!code-vbFormatting.Enum#3]
X 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. The following example illustrates the X format specifier.

[!code-csharpFormatting.Enum#4] [!code-vbFormatting.Enum#4]

Example

The following example defines an enumeration called Colors that consists of three entries: Red, Blue, and Green.

[!code-csharpFormatting.Enum#5] [!code-vbFormatting.Enum#5]

After the enumeration is defined, an instance can be declared in the following manner.

[!code-csharpFormatting.Enum#6] [!code-vbFormatting.Enum#6]

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.

[!code-csharpFormatting.Enum#7] [!code-vbFormatting.Enum#7]

See Also

Formatting Types