Note to myself: Enum Default Value
When defining enums, I define the first value as unspecified to have a compiler-save representation for example of a field or a variable:
public enum ValueValidationState
{
Unspecified,
CanBecomeValid,
Valid,
Invalid
}
private field ValueValidationState _validationState;
void ValidateValue(string value)
{
ValueValidationState validationState;
if (...)
{
validationState = ValueValidationState.CanBecomeValid;
}
else if (//...
return validationState;
}
Behind the scenes, the enum is an integer. Integers, like other primitive number-values, are initialized with 0. Without defining a first value for the enum, the first value (Unspecified) equals 0, leading to a valid initialized _validationState
of ValueValidationState.Unspecified
without assigning an explicit case. The IDE and/or compiler might warn about the use of an uninitialized field or variable, but I’m on the safe side with this. It also helps when using (de)serialization.