C# 枚举:可空或“未知”价值?

发布于 2024-08-12 05:17:55 字数 334 浏览 2 评论 0原文

如果我有一个带有 enum 成员的类,并且我希望能够表示未定义该成员的情况,那么哪个更好?

a) 使用可为空类型在类中将成员声明为可为空。例如:

public SomeEnum? myEnum;

b) 向枚举添加默认的“未知”值。例如:

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

我真的看不出任何主要的优点/缺点;但也许其中一个比另一个更好?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情绪失控 2024-08-19 05:17:55

绝对使用可为空的值类型 - 这就是它们的用途。它明确地表明了您的意图。这也意味着您可以使用 Enum.IsDefined (如果您想要泛型类型安全,则可以使用 Unconstrained Melody 中的等效项)轻松确定特定值是否为真实值,而不必担心“假”值。

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.

情归归情 2024-08-19 05:17:55

我同意 archimed7592 的观点,即缺席值和“未知”值之间存在真正的区别。

例如:

“你的血型是什么?”

  • Null = 缺少值 -->问题尚未得到解答 -->提出问题

  • “未知”-->病人表示他不知道 -->订购血型实验室测试

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

海未深 2024-08-19 05:17:55

您只需决定是否需要一个值来表示未知值,或者确实需要一种方法来表示任何值的缺失。

在需要表示未知值的情况下,额外的枚举成员听起来是一个不错的解决方案。

如果需要表示不存在任何值,请将其设为可为空。

请记住,“未知”枚举成员和枚举本身同时可为空并没有什么问题。

HTH。

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.

爱她像谁 2024-08-19 05:17:55

视情况而定!

Bill Wagner 列出了一些在有意义时添加未定义枚举的充分理由。我建议您找到完整的项目,但这里是预览:

第 8 项:确保 0 是值类型的有效状态

默认的.NET系统初始化将所有对象设置为全0。您无法阻止其他程序员创建初始化为全 0 的值类型的实例。将其设为您的类型的默认值。

一个特殊情况是枚举。切勿创建不包含 0 作为有效选择的枚举。所有枚举均派生自 System.ValueType。枚举的值从 0 开始,但您可以修改该行为...

现在,我遇到一种情况,用户需要从组合框中选择某种类型,或者他们可以不选择任何类型。我使用带有 [Description("a description")] 属性的枚举作为要选择的对象。如果“none/Unknown”是枚举的自然选择,那么我将用它来表示没有选择任何内容。否则,我将使用 Nullable

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

梨涡 2024-08-19 05:17:55

如果它可以为空,您将把它作为默认值,如果您尝试在空时使用它,您会得到异常(这是一件好事!)

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

七禾 2024-08-19 05:17:55

如果枚举具有 [Flags] 属性,则一切都会改变。

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

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

    [Flags]
    enum FlagsEnum
    {
        None = 0,
        First = 1,
        Second = 2,
        Third = 4,
        FirstAndThird = 5
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文