c# check enum is contained in options
This question already has an answer here:
The simplest way is to use &
:
if ((available & me) != 0)
You can use 0 here as there's an implicit conversion from the constant 0 to any enum, which is very handy.
Note that your enum should be defined using the Flags
attribute and appropriate bit-oriented values though:
[Flags]
public enum Fruits
{
Apple = 1 << 0,
Orange = 1 << 1,
Grape = 1 << 2,
Ananas = 1 << 3,
Banana = 1 << 4
}
If you don't want to make it a Flags
enum, you should use a List<Fruit>
or similar to store available options.
上一篇: 整数在C#中枚举铸造
下一篇: c#check enum包含在选项中