CSharp - Enum Format Strings

Introduction

The following table lists each format string and the result of applying it to the following expression:

Format string Meaning
Sample output Notes
G or g

"General"

Red

Default
Works on combined members even if
F or f

Treat as though Flags
attribute were present
Red


enum has no Flags attribute
D or d
Decimal value
12
Retrieves underlying integral value
X or x
Hexadecimal value
0000000C
Retrieves underlying integral value

Demo

using System;
class MainClass/* w ww. j  a  va2 s  .co  m*/
{
    public static void Main(string[] args)
    {
        Console.WriteLine(System.ConsoleColor.Red.ToString("D"));
    }
}

Result