Enumerators enable you to create variables that contain a limited number of values. - CSharp Custom Type

CSharp examples for Custom Type:Enum

Introduction

The format of creating an enumerator is as follows:

modifiers enum enumName
{
    enumMember1,
    enumMember2,
    ...
    enumMemberN
}

The following declares a toggle enumerator with public access:

public enum toggle
{
   On,Off
}

Demo Code

using System;//from  w ww .ja  v a  2 s. c  om
class MainClass
{
   enum Color
   {
      red,
      white,
      blue
   }
   public static void Main()
   {
      string buffer;
      Color myColor;
      Console.Write( "Enter a value for a color: 0 = Red, 1 = White, 2 = Blue): ");
      buffer = Console.ReadLine();
      myColor = (Color) Convert.ToInt32(buffer);
      switch( myColor )
      {
         case Color.red:
         System.Console.WriteLine("\nSwitched to Red...");
         break;
         case Color.white:
         System.Console.WriteLine("\nSwitched to White...");
         break;
         case Color.blue:
         System.Console.WriteLine("\nSwitched to Blue...");
         break;
         default:
         System.Console.WriteLine("\nSwitched to default...");
         break;
      }
      System.Console.WriteLine("\nColor is {0} ({1})", myColor, (int) myColor);
   }
}

Result


Related Tutorials