#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

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #457 – Converting Between enums and their Underlying Type

  1. Pingback: #459 – Assigning a Value of a Different Type to an enum « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: