#457 – Converting Between enums and their Underlying Type

When you declare an enum, by default each enumerated value is represented internally with an int.  (System.Int32 – 4 bytes).  You can convert between values of the underlying type and enum values using an explicit cast.  Because an enum is represented by an int by default, you can convert between integers and enum values.

            Mood m = (Mood)2;
            Console.WriteLine(m);   // Petulant

            m = Mood.Crabby;
            int i = (int)m;
            Console.WriteLine(i);   // 0

Advertisements