Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

null

enums

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?

like image 917
MHOOS Avatar asked Jun 10 '14 21:06

MHOOS


People also ask

Can we add null in enum?

NOT NULL − In ENUM type, by default NULL values are allowed. To disallow NULL values, we need to use the NOT NULL attribute while describing the ENUM column. NULL − The NULL attribute is a synonym for DEFAULT NULL. The index value for NULL is NULL.

Should enums be constants?

Enums are strongly-typed constants that make the code more readable and less prone to errors. It is useful when you have a set of values that are functionally significant and unchanged. An enumeration is a user-defined data type consisting of integral constants and each integral constant is given a name.

Can enum be null SQL?

Empty or NULL Enumeration ValuesAn enumeration value can also be the empty string ( '' ) or NULL under certain circumstances: If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value.

Do enum values have to be unique?

CA1069: Enums should not have duplicate values.


2 Answers

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.

like image 68
Mike Zboray Avatar answered Oct 01 '22 01:10

Mike Zboray


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.

like image 26
Cᴏʀʏ Avatar answered Oct 01 '22 00:10

Cᴏʀʏ