Use enumerations

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet illustrates the declaration and use of enumerated types and the C# enum keyword.

enum Planets {   // base type is int
{
   Mercury,      // default value 0
   Venus,
   Earth,
   Mars,
   Jupiter,
   Saturn,
   Uranus,
   Neptune,
   Pluto
}
 
enum Moons : short    // base type is short
{
   Mercury =  0,
   Venus   =  0,
   Earth   =  1,
   Mars    =  2,
   Jupiter = 16,
   Saturn  = 18,
   Uranus  = 15,
   Neptune =  8,
   Pluto   =  1
}
 
public static void Main()
{
   Console.WriteLine ("The planet {0} has {1} moons.", 
      Planets.Jupiter, (short) Moons.Jupiter);
}

Personal tools