如何测试枚举是否在集合中 [英] How to test if an enum is inside a set

查看:99
本文介绍了如何测试枚举是否在集合中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我讨厌这种语法


MyEnum aEnum;

...

if((aEnum == MyEnum.Green)||(aEnum == MyEnum.Red)||(aEnum ==

MyEnum.Yellow))...


在delphi中我们有类似


的东西,如果aEnum在[Green,Red,Yellow]然后开始

end;


我在C#中寻找类似的东西。

我找不到它。有人已经找到了吗?


亲切的问候


亚历山大

Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I''m looking for something like this in C#.
I can''t find it. Anybody has found it already?

Kind regards

Alexander

推荐答案

亚历山大,


如果你想看看这个值是否是枚举中

值的一个子集,那么你将拥有按照你的方式去做(或者把
放在一个数组然后搜索一下)。


但是,如果设置你正在查看的是自己的枚举,

然后您可以在Enum类上调用静态IsDefined方法来确定

该值是否在枚举中。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

Alexander Muylaert ; <回复******** @ pandora.be>在消息中写道

新闻:ui ************** @ TK2MSFTNGP09.phx.gbl ...
Alexander,

If you are looking to see if the value is in a subset of the values in
an enumeration, then you will have to do it the way you are doing it (or
place the subset in an array and then search through that).

However, if the set you are looking through is the enumeration yourself,
then you can call the static IsDefined method on the Enum class to determine
whether or not the value is in the enumeration.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alexander Muylaert" <Re********@pandora.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...


我讨厌这种语法

MyEnum aEnum;
...
if((aEnum == MyEnum.Green)||(aEnum == MyEnum.Red) ||(aEnum ==
MyEnum.Yellow))...

如果在[绿色,红色,黄色]中有aEnum,在delphi中我们会有类似的东西

然后开始结束;

我在C#中寻找这样的东西。
我找不到它。有人已经找到了吗?

亲切的问候

亚历山大
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I''m looking for something like this in C#.
I can''t find it. Anybody has found it already?

Kind regards

Alexander



把如果((aEnum&(Green | Red | Yellow))!= 0)

....


必须确保枚举的值类似于0x01,0x02,0x10,0x20,

等。


Alexander Muylaert <回复******** @ pandora.be>在消息中写道

新闻:ui ************** @ TK2MSFTNGP09.phx.gbl ...
Put the FlagsAttribute thingy on the enum, and then have
if((aEnum & (Green | Red | Yellow)) != 0)
....

have to make sure the values of the enum are like 0x01, 0x02, 0x10, 0x20,
etc.

"Alexander Muylaert" <Re********@pandora.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...


我讨厌这种语法

MyEnum aEnum;
...
if((aEnum == MyEnum.Green)||(aEnum == MyEnum.Red) ||(aEnum ==
MyEnum.Yellow))...

如果在[绿色,红色,黄色]中有aEnum,在delphi中我们会有类似的东西

然后开始结束;

我在C#中寻找这样的东西。
我找不到它。有人已经找到了吗?

亲切的问候

亚历山大
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I''m looking for something like this in C#.
I can''t find it. Anybody has found it already?

Kind regards

Alexander



Alexander,


没有直接的等价物。但是,类似下面的东西应该

让事情变得更方便:

public bool EnumInList(Enum target,params Enum [] allowedValues)

{

bool found = false;


for(int i = 0; i< allowedValues.Length; i ++)

{

if(Enum.Equals(allowedValues [i],target))

{

found = true;

休息;

}

}


返回找到;

}


为了获得更好的性能,请使用您的特定枚举类型而不是Enum

基类。


HTH ,

Nicole

" Alexander Muylaert" <回复******** @ pandora.be>在消息中写道

新闻:ui ************** @ TK2MSFTNGP09.phx.gbl ...
Alexander,

There''s no direct equivalent. However, something like the following should
make things a bit more convenient:

public bool EnumInList(Enum target, params Enum[] allowedValues)
{
bool found = false;

for (int i = 0; i < allowedValues.Length; i++)
{
if (Enum.Equals(allowedValues[i], target))
{
found = true;
break;
}
}

return found;
}

For better performance, use your particular enum type rather than the Enum
base class.

HTH,
Nicole
"Alexander Muylaert" <Re********@pandora.be> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...


我讨厌这种语法

MyEnum aEnum;
...
if((aEnum == MyEnum.Green)||(aEnum == MyEnum.Red) ||(aEnum ==
MyEnum.Yellow))...

如果在[绿色,红色,黄色]中有aEnum,在delphi中我们会有类似的东西

然后开始结束;

我在C#中寻找这样的东西。
我找不到它。有人发现了吗?

亲切的问候

亚历山大
Hi

I hate this syntax

MyEnum aEnum;
...
if ((aEnum == MyEnum.Green) || (aEnum == MyEnum.Red) || (aEnum ==
MyEnum.Yellow)) ...

In delphi we have something like

if aEnum in [Green, Red, Yellow] then begin
end;

I''m looking for something like this in C#.
I can''t find it. Anybody has found it already?

Kind regards

Alexander



这篇关于如何测试枚举是否在集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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