#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
        };
Advertisements