UpTheCreek
UpTheCreek

Reputation: 32391

C# Enums: Nullable or 'Unknown' Value?

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?

Upvotes: 82

Views: 38921

Answers (6)

LuckyLikey
LuckyLikey

Reputation: 3840

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

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

Upvotes: 1

Hamish Grubijan
Hamish Grubijan

Reputation: 10820

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>.

Upvotes: 5

Maarten jansonius
Maarten jansonius

Reputation: 299

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

Upvotes: 29

HolisticElastic
HolisticElastic

Reputation: 967

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.

Upvotes: 9

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!)

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500675

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.

Upvotes: 76

Related Questions