#203 – It’s Good Practice to Always Have a 0-Valued Enumeration Constant

We saw earlier that you can declare any values you like for an enum type’s constants.

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

Clearly we can declare an enum type that has no constant that maps to the value of 0.

We saw earlier, however, that fields in a reference type are zeroed out when an instance of that object is constructed.  This can lead to having an enumerated field/property that has a 0 value but no matching constant in the enumerated type.

            Person p = new Person("Lillian", "Gish");

            Mood theMood = Mood.Happy;
            Console.WriteLine(theMood);    // Happy

            theMood = p.PersonMood;
            Console.WriteLine(theMood);    // 0   (unable to map to a constant)

This is allowed, since the enumerated value can be any integer.  But it’s better practice to always define a 0-valued constant.

    public enum Mood
        NONE = 0,
        Crabby = -5,
        Happy = 5,
        Petulant = -2,
        Elated = 10