【问题标题】:Boolean expression involving Enum and Int [duplicate]涉及 Enum 和 Int 的布尔表达式
【发布时间】:2017-03-08 08:47:06
【问题描述】:

我有一个带有几个值的简单枚举

Enum Status{
Available, 
Taken,
Sold//and many more
}

我想检查状态是否介于可能值之间(假设值的写入顺序最低的是流程的开始,最后的是流程的最后步骤)

类似

Status s1;//Some value
if(s1<5 && s1>3)
//The value is one of the enum values in those range
//(for example the job has already been accepted, but has still not been
//shipped)

有可能吗?

谢谢。

【问题讨论】:

  • 只需将状态转换为 int 并进行检查:(int)s1
  • 有人删除了这个问题,我不知道如何正确搜索。
  • 您可以自行删除。编辑旁边有一个按钮。我们不会为你做这件事。
  • 我不能,因为已经有答案了。
  • 应该是可能的,因为答案是 0 分。你得到的信息是什么?

标签: c#


【解决方案1】:

您可以将整数值分配给您的枚举,然后检查。

enum Status
{
    Available = 1, 
    Taken = 2,
    Sold = 3
    //and many more
}

Status s1; // any value
if ((int)s1 <= 5 && (int)s1 >= 3)

【讨论】:

  • 嗯,因为枚举默认是整数,我猜不分配数字已经把它们按顺序排列了,对吗?
  • @master2080 正确,从索引 0 开始。
猜你喜欢
  • 2019-01-30
  • 2019-04-26
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 2015-08-18
相关资源
最近更新 更多