Displaying Values from Enumerations - CSharp Custom Type

CSharp examples for Custom Type:Enum

Introduction

SpecifierDescription
D or d Displays the numeric value from the enumerator element
G or g Displays the string value from the enumerator element
X or x Displays the hexadecimal equivalent of the numeric value from the enumerator element

Demo Code

using System;//from  ww  w .j  a v  a 2s. c  o m
class Enums
{
   enum Pet
   {
      Cat,
      CSS,
      XML,
      Dog,
      Fish,
      Snake,
      Rat,
      Hamster,
      Bird
   }
   public static void Main()
   {
      Pet myPet = Pet.Fish;
      Pet yourPet = Pet.Hamster;
      Console.WriteLine("Using myPet: ");
      Console.WriteLine("d: {0:d}", myPet );
      Console.WriteLine("D: {0:D}", myPet );
      Console.WriteLine("g: {0:g}", myPet );
      Console.WriteLine("G: {0:G}", myPet );
      Console.WriteLine("x: {0:x}", myPet );
      Console.WriteLine("X: {0:X}", myPet );
      Console.WriteLine("\nUsing yourPet: ");
      Console.WriteLine("d: {0:d}", yourPet );
      Console.WriteLine("D: {0:D}", yourPet );
      Console.WriteLine("g: {0:g}", yourPet );
      Console.WriteLine("G: {0:G}", yourPet );
      Console.WriteLine("x: {0:x}", yourPet );
      Console.WriteLine("X: {0:X}", yourPet );
   }
}

Result


Related Tutorials