检查是否所有 Enum 值均未设置

发布于 2024-11-23 16:12:21 字数 115 浏览 6 评论 0原文

是否可以使用 ONE-Liner (其他正常方式...)来检查是否设置了所有可能的枚举状态?

从枚举水果中我想知道它是否未设置意味着没有香蕉,没有苹果,没有瓜。

我怎样才能做到这一点?

Is it possible with a ONE-Liner (else the normal way...) to check if any of all possible enum states are set?

From enum fruits I want to know wether it is unset means no banana, no apple, no melon.

How can I do that?

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

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

发布评论

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

评论(4

唔猫 2024-11-30 16:12:21

myFlags == 0

足够了吗? 0 表示没有设置位,假设您正在使用标志并且没有对这些值执行任何奇怪的操作。

给定这个枚举:

[Flags]
enum Fruit
{
    Forbidden = 0,
    Apple = 1,
    Orange = 2,
    Banana = 4
}

并且这段代码:

Fruit f = Fruit.Apple | Fruit.Banana;

if (f == 0)
    MessageBox.Show("what?");

f = Fruit.Forbidden;

if (f == 0)
    MessageBox.Show("expected");

它执行您期望的操作,但是它检查底层值而不执行按位操作,因此,如果您错误地改变底层值,您就会开始遇到问题,但您会得到其他问题问题也是如此。

鉴于:

[Flags]
enum Fruit
{
    Apple = 1,
    Orange = 2,
    Banana = 4
}

并且:

Fruit f = Fruit.Apple | Fruit.Banana;

if (f == 0)
    MessageBox.Show("what?");

f = f & ~Fruit.Apple & ~Fruit.Banana;

if (f == 0)
    MessageBox.Show("expected");

也表现良好。

Would:

myFlags == 0

Suffice? 0 represents no bits set, assuming you are using flags and not doing anything funky with the values.

Given this enum:

[Flags]
enum Fruit
{
    Forbidden = 0,
    Apple = 1,
    Orange = 2,
    Banana = 4
}

And this code:

Fruit f = Fruit.Apple | Fruit.Banana;

if (f == 0)
    MessageBox.Show("what?");

f = Fruit.Forbidden;

if (f == 0)
    MessageBox.Show("expected");

It does what you'd expect it to, however it is checking the underling value not doing bitwise ops, so if you mutate the underling value incorrectly you start to get issues, but you'll get other issues as well.

Given:

[Flags]
enum Fruit
{
    Apple = 1,
    Orange = 2,
    Banana = 4
}

And:

Fruit f = Fruit.Apple | Fruit.Banana;

if (f == 0)
    MessageBox.Show("what?");

f = f & ~Fruit.Apple & ~Fruit.Banana;

if (f == 0)
    MessageBox.Show("expected");

Also behaves.

稳稳的幸福 2024-11-30 16:12:21
[Flags]
enum Fruit
{
    None,
    Apple  /* = 1*/,
    Orange    = Apple << 1,
    Banana    = Orange << 1,

    All       = (Banana << 1) -1
}

通过这种方式,您可以检查是否

  • 未设置水果标志:value == Fruit.None
  • 所有水果标志已设置:value == Fruit.All
  • 设置了非法水果值:(value & ~Fruit.All) != 0

请参阅 IDEOne

[Flags]
enum Fruit
{
    None,
    Apple  /* = 1*/,
    Orange    = Apple << 1,
    Banana    = Orange << 1,

    All       = (Banana << 1) -1
}

This way you could check whether

  • no fruit flags are set: value == Fruit.None
  • all fruit flags are set: value == Fruit.All
  • illegal fruit values are set: (value & ~Fruit.All) != 0

See this on IdeOne

温柔戏命师 2024-11-30 16:12:21

假设您正在谈论设置为位标志的枚举,MSDN 有一个很好的例子

[Flags]
enum Days2
{
    None = 0x0,
    Sunday = 0x1,
    Monday = 0x2,
    Tuesday = 0x4,
    Wednesday = 0x8,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}

要检查是否设置了位标志,您可以这样做:

// Test value of flags using bitwise AND.
bool test = (meetingDays & Days2.Thursday) == Days2.Thursday;

理论上,您可以将所有按位操作放在一行上,并用 &| 取决于您的意图。

Assuming you are talking about an enumeration that is setup as Bit Flags, MSDN has a good example:

[Flags]
enum Days2
{
    None = 0x0,
    Sunday = 0x1,
    Monday = 0x2,
    Tuesday = 0x4,
    Wednesday = 0x8,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}

to check whether a bit flag is set, you could, for example do this:

// Test value of flags using bitwise AND.
bool test = (meetingDays & Days2.Thursday) == Days2.Thursday;

In theory, you could put all of your bitwise operations on one line separated by an & or | depending on your intent.

就是爱搞怪 2024-11-30 16:12:21

我建议枚举包含一个默认的“非设置”成员,例如值为 00x0Fruit.None。这样,任何具有枚举 Fruit 类型的“非设置”成员都可以默认设置为 Fruit.None

public enum Fruit
{
    None,
    Banana,
    Apple,
    Kumquat,
}

public class Meal
{
    Fruit myFruit = Fruit.None;
}

I would recommend that the enum contain a default, "non set" member such as Fruit.None with the value of 0 or 0x0. That way, any "non set" member with the type of enum Fruit could be set as default to Fruit.None.

public enum Fruit
{
    None,
    Banana,
    Apple,
    Kumquat,
}

And

public class Meal
{
    Fruit myFruit = Fruit.None;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文