#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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: