#191 – Changing the Underlying Type of an enum

When you declare an enum, by default each enumerated value is represented internally with an int.  (System.Int32 – 4 bytes).  But you can use any of the following types: byte, sbyte, short, ushort, uint, long, or ulong.

        public enum Mood { Crabby, Happy, Petulant, Elated };   // type is int

        public enum MoodByte : byte { Crabby, Happy, Petulant, Elated };

        static void Main()
        {
            // 4 bytes
            Console.WriteLine("Each element is {0} bytes", sizeof(Mood));

            // 1 byte
            Console.WriteLine("Each element is {0} bytes", sizeof(MoodByte));
        }
Advertisements

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

One Response to #191 – Changing the Underlying Type of an enum

  1. Peter Chamberlin says:

    Typo: “// 4 bytets” → ‘bytes’

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 )

Google+ photo

You are commenting using your Google+ 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: