#192 – Using Non-Default Constant Values for Enumeration Members

When you define an enum type, the members contained in your enumeration take on constants starting at zero (for the first member) and then incrementing by one (for consecutive members).

        public enum Mood {
            Crabby,       // 0
            Happy,        // 1
            Petulant,     // 2
            Elated };     // 3

You can, however, specify the constant value to use for each member.  You can specify constants in any order.  Members that don’t have a value specified take on a value one greater than the previous member.

        public enum Mood { 
            Crabby = -5,       
            Happy = 5,        
            Petulant = -2,     
            Elated = 10};

        public enum Weekday
        {
            Sunday = 1,
            Monday,     // 2
            Tuesday,    // 3, etc.
            Wednesday,
            Thursday,
            Friday,
            Saturday
        };

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 #192 – Using Non-Default Constant Values for Enumeration Members

  1. Pingback: #203 – It’s Good Practice to Always Have a 0-Valued Enumeration Constant « 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

Follow

Get every new post delivered to your Inbox.

Join 43 other followers