Convert enum to int : enum base type « Data Type « C# / CSharp Tutorial






using System;

enum Color
{
   Green,
   Yellow,
   Red
}

class MainClass
{
   static void Main()
   {
      Color t1 = Color.Green;
      Color t2 = Color.Yellow;
      Color t3 = Color.Red;

      Console.WriteLine("{0},\t{1}",   t1, (int)t1);
      Console.WriteLine("{0},\t{1}",   t2, (int)t2);
      Console.WriteLine("{0},\t{1}\n", t3, (int)t3);
   }
}
Green,  0
Yellow, 1
Red,    2








2.37.enum base type
2.37.1.Convert enum to int
2.37.2.Assign int value to all elements in an enum
2.37.3.Assign int value to the first element in an enum
2.37.4.Define enumeration base types
2.37.5.Bit Flag Enums
2.37.6.Using Bit flags when declaring the enum
2.37.7.Print out string version of an enum value
2.37.8.Using a base type of long when defining an enum
2.37.9.Using Operators with Enumerations
2.37.10.enum based on byte
2.37.11.enum based on byte, and convert enum variable back to byte