Consider the following Enum and a corrsponding nullable field of that type
enum PossibleOptions { One, Two }
PossibleOptions? option;
Alternatively I could declare the enum and the corresponding field as
enum PossibleOptions { Unspecified, One, Two }
PossibleOptions option;
This non-nullable field would be initialized to the first value i.e ‘Unspecified’ and I achieve the same result as a nullable (‘Unspecified’ would replace option.HasValue).
Why go for a Nullable then? Any performance gains or other advantages?
According to the documentation:
There is also possibility to modify this default value:
In this case
Unspecifiedwill no longer be the default value.The only possible advantage that I can see of using a nullable enum is that the null value will not be dependent on the definition of the enumeration.
In my opinion you should decide which one to use depending on whether you need the semantics of default value or unassigned value.