C# Enums: Nullable or 'Unknown' Value?

C#Enums

C# Problem Overview


If I have a class with an enum member and I want to be able to represent situations where this member is not defined, which is it better?

a) Declare the member as nullable in the class using nullable types. E.g.:

public SomeEnum? myEnum;

b) Add a default, 'unknown' value to the enumeration. E.g.:

public enum SomeEnum {
	Unknown,
	SomeValueA,
	SomeValueB,
	SomeValueC,
}

I can't really see any major pros/cons either way; but perhaps one is preferable over the other?

C# Solutions


Solution 1 - C#

Definitely use a nullable value type - that's what they're for. It explicitly states your intention. It also means you can use Enum.IsDefined (or the equivalent from Unconstrained Melody if you want generic type safety) to easily determine whether a particular value is a real value without worrying about the "fake" one too.

Solution 2 - C#

I agree with archimed7592 that there is a real difference between absent value and "Unknown" value.

Example:

"what is your blood type?"

  • Null = absent value --> question hasn't been answered --> ask the question

  • "Unknown" --> patient indicates he doesn't know --> order lab test for the blood type

Solution 3 - C#

You just have to decide whether you need a value to represent unknown values, or you do need a way to represent absence of any value.

In the case of a need to represent unknown values, an extra enum-member sounds good as a solution.

In case of a need to represent absence of any value, make it nullable.

Have in mind, that there is nothing wrong with having both "unknown" enum-member and the enum itself being nullable at the same time.

HTH.

Solution 4 - C#

IT DEPENDS!

Bill Wagner has listed some good reasons to add an Undefined enum when it makes sense. I suggest that you find the full item, but here's a preview:

>Item 8: Ensure That 0 Is a Valid State for Value Types > >The default .NET system initialization sets all objects to all 0s. There is no way for you to prevent other programmers from creating an instance of a value type that is initialized to all 0s. Make that the default value for your type. > >One special case is enums. Never create an enum that does not include 0 as a valid choice. All enums are derived from System.ValueType. The values for the enumeration start at 0, but you can modify that behavior...

Now, I have a situation where the users need to select some type from a ComboBox, or they can have no type selected. I am using an enum with a [Description("a description")] attribute as the object to be selected. If none/Unknown is a natural choice for an enum, then I would use that to represent that nothing is selected. Otherwise, I will use a Nullable<MyEnum>.

Solution 5 - C#

If it's nullable, you'd get it as a default value, you'd get exceptions if you tried to use it when null (which is a good thing!)

Solution 6 - C#

Everything changes if the enum is attributed with [Flags] Attribute.

	[Flags]
	enum FlagsEnum
	{
		None = 0,
		First = 1,
		Second = 2,
		Third = 4,
		FirstAndThird = 5
	}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUpTheCreekView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Maarten jansoniusView Answer on Stackoverflow
Solution 3 - C#archimed7592View Answer on Stackoverflow
Solution 4 - C#Hamish GrubijanView Answer on Stackoverflow
Solution 5 - C#Fiona - myaccessible.websiteView Answer on Stackoverflow
Solution 6 - C#LuckyLikeyView Answer on Stackoverflow