开发者

C# Enums: Nullable or 'Unknown' Value?

开发者 https://www.devze.com 2022-12-12 23:47 出处:网络
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?

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 en开发者_运维知识库um SomeEnum {
    Unknown,
    SomeValueA,
    SomeValueB,
    SomeValueC,
}

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


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.


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


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.


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


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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号