C# 枚举:可空或“未知”价值?
如果我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
绝对使用可为空的值类型 - 这就是它们的用途。它明确地表明了您的意图。这也意味着您可以使用
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.我同意 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
您只需决定是否需要一个值来表示未知值,或者确实需要一种方法来表示任何值的缺失。
在需要表示未知值的情况下,额外的枚举成员听起来是一个不错的解决方案。
如果需要表示不存在任何值,请将其设为可为空。
请记住,“未知”枚举成员和枚举本身同时可为空并没有什么问题。
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.
视情况而定!
Bill Wagner 列出了一些在有意义时添加未定义
枚举
的充分理由。我建议您找到完整的项目,但这里是预览:现在,我遇到一种情况,用户需要从组合框中选择某种类型,或者他们可以不选择任何类型。我使用带有
[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: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 aNullable<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!)
如果枚举具有
[Flags]
属性,则一切都会改变。Everything changes if the enum is attributed with
[Flags]
Attribute.