Is it a good practice to add a “Null” or “None” member to the enum? [duplicate]

Posted on

Problem :

When creating a new enum in C#, is it a good practice to have null member?

If yes, do you give it the value of 0 by default? Would you call the null member Null or NULL? Do you strictly believe in Null or you don’t have any problem with calling it something other than null. For instance None. Can you elaborate on your answer?

Solution :

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.

The Framework Design Guidelines suggest that you should:

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.

Example:

 public enum Compression {
      None = 0,
      GZip,
      Deflate
 }

They also advise against using sentinal values like First and Last, since they are confusing for users.

These are only guidelines and there may be competing concerns that override this advice, like compatibility with existing libraries or usage patterns.

Leave a Reply

Your email address will not be published. Required fields are marked *