Changing the Underlying Type of an Enumerator - CSharp Custom Type

CSharp examples for Custom Type:Enum

Introduction

To change the default type, you use the following format:

modifiers enum enumName : typeName { member(s) }

Demo Code

using System;//from   ww  w.  j a  v  a2s  .  co m
class MainClass
{
   enum Color : byte
   {
      red,
      white,
      blue
   }
   public static void Main()
   {
      Color myColor;
      byte   roll;
      System.Random rnd = new System.Random();
      for ( int i = 0; i < 10; i++ )
      {
         roll = (byte) (rnd.Next(0,3)); // random nbr from 0 to 2
         myColor = (Color) roll;
         System.Console.WriteLine("Color is {0} ({1} of type {2})", myColor, (byte) myColor, myColor.GetTypeCode());
      }
   }
}

Result


Related Tutorials