A format specifier can quickly convert an enumerated type into a properly formatted value string:
enum enumType { TypeA = 1, TypeB = 2, TypeC = 3 } enumType eVal = enumType.TypeB; Console.WriteLine( "{0:g} [" + string.Format( "{0:g}", eVal ) + "]" );
Table C-3 shows how to format an enumeration that takes a value of enumType.TypeB.
SPECIFIER | TYPE | FORMAT | OUTPUT |
---|---|---|---|
G | Default (flag names if available, otherwise decimal) | {0:G} | TypeB |
F | Flags always | {0:F} | TypeB |
D | Integer always | {0:D} | 2 |
X | Eight-digit hex | {0:X} | 00000002 |