【发布时间】:2015-10-15 11:26:11
【问题描述】:
描绘场景。
public enum SaveStates
{
Saved, //Represents a successful save.
SavedWithChanges, //Represents a successful save, where records were modified
SavedWithoutChanges //Represents a successful save, but no records were modified
}
在这种情况下,如果枚举是SavedWithChanges 或 SavedWithoutChanges,则可以认为枚举是Saved。
如果我有这样的变量:
SaveStates lastState = SaveStates.SavedWithoutChanges;
理想情况下,我想做这样的事情:
if (lastState == SaveStates.Saved)
{
//The state is saved, do something awesome.
}
我当然可以这样做:
if (lastState == SaveStates.SavedWithChanges || lastState == SaveStates.SavedWithoutChanges)
{
...
但是这有点乏味,我不能假设其他开发人员会理解如何正确使用枚举。
每个枚举都是必需的,因为在某些情况下,我们可能想要在没有更改的保存事件中做一些特定的事情。
我对替代设计想法持开放态度。
【问题讨论】:
-
我认为其他开发人员不会理解这一点。 Imo 这比使用 flags 属性更清楚。
-
你的枚举不应该有一个未保存的值吗?否则为什么要检查它是否已保存,您只需使用
Enum.IsDefined。