UPDATE: The widely accepted answer on the linked duplicate favors the use of nullable types instead of defining a "null"/"none"/0 member. The FxCop rule described below existed before Nullable
types did.
So instead of:
var myEnum = SomeEnumType.None;
You would defined it as null
using a Nullable
value type instead:
SomeEnumType? myEnum = null;
I still hold that a None
member of a [Flags]
enum can be useful.
Previous answer:
There's a design rule in Visual Studio, CA1008, that provides some insight into your question. The description of the rule is (styling mine):
The default value of an uninitialized enumeration, just like other value types, is zero. A non-flags−attributed enumeration should define a member that has the value of zero so that the default value is a valid value of the enumeration. If appropriate, name the member 'None'. Otherwise, assign zero to the most frequently used member. Note that, by default, if the value of the first enumeration member is not set in the declaration, its value is zero.
If an enumeration that has the FlagsAttribute
applied defines a zero-valued member, its name should be 'None' to indicate that no values have been set in the enumeration. Using a zero-valued member for any other purpose is contrary to the use of the FlagsAttribute
in that the AND and OR bitwise operators are useless with the member. This implies that only one member should be assigned the value zero. Note that if multiple members that have the value zero occur in a flags-attributed enumeration, Enum.ToString()
returns incorrect results for members that are not zero.
There is also an Enum Design article that makes the following points:
- √ DO provide a value of zero on simple enums. Consider calling the value something like "None." If such a value is not appropriate for this particular enum, the most common default value for the enum should be assigned the underlying value of zero.
- X AVOID using flag enum values of zero unless the value represents "all flags are cleared" and is named appropriately, as prescribed by the next guideline.
- √ DO name the zero value of flag enums
None
. For a flag enum, the value must always mean "all flags are cleared."
Based on the above, I would say that yes, it is good practice, especially when you have a [Flags]
enum
.