枚举应该有未初始化值。 [英] Should enums have uninitialized values .

查看:556
本文介绍了枚举应该有未初始化值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个辩论,如果枚举应该有未初始化值。例如。我们有

We were having a debate if enums should have uninitialized values. For example. We have

public enum TimeOfDayType
{
   Morning
   Afternoon
   Evening
}

public enum TimeOfDayType
{
   None
   Morning
   Afternoon
   Evening
}

我认为不应该有任何没有,但随后你不得不默认为在初始化一些有效的价值。但也有人认为应该由有另一个枚举是无或NotSet uniitized状态的指示。

I think that there shouldn't be any none but then you have to default to some valid value on initialization. But others thought there should be some indication of uniitized state by having another enum that is None or NotSet.

想法?

推荐答案

可空类型说到 - 我认为他们可以用来解决强迫/强制不枚举的初始化的问题。假设我们有

Speaking of nullable types - I think they can be used to solve the problem of forcing/not forcing the initialization of an enum. Say we have

enum Color { Red, Blue }

和假设你有一个函数:

void Draw(Color c);

这功能说,它的要求有效颜色。然而,我们也可以有这样的功能:

That function says that it requires a valid Color. However, we could also have this function:

void Draw(Color? c);

这表示该函数不能处理传递一种颜色(将被传递表示不关心)。

That says that the function can handle not being passed a color (null would be passed to indicate "don't care").

嗯,这是一种可以替代的成员。

Well, it's one alternative to None members.

这篇关于枚举应该有未初始化值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆