We saw earlier that you can declare any values you like for an enum type’s constants.
1
2
3
4
5
| 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.
1
2
3
4
5
6
7
| Person p = new Person("Lillian", "Gish");Mood theMood = Mood.Happy;Console.WriteLine(theMood); // HappytheMood = 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.
1
2
3
4
5
6
7
8
| public enum Mood{ NONE = 0, Crabby = -5, Happy = 5, Petulant = -2, Elated = 10}; |

