Set In enum for C #

I would like to find a way to check if a set of values ​​is contained in my variable.

[Flags]
public enum Combinations
{
    Type1CategoryA = 0x01,      // 00000001
    Type1CategoryB = 0x02,      // 00000010
    Type1CategoryC = 0x04,      // 00000100
    Type2CategoryA = 0x08,      // 00001000
    Type2CategoryB = 0x10,      // 00010000
    Type2CategoryC = 0x20,      // 00100000
    Type3 = 0x40                // 01000000
}

bool CheckForCategoryB(byte combinations)
{

    // This is where I am making up syntax

    if (combinations in [Combinations.Type1CategoryB, Combinations.Type2CategoryB])
        return true;
    return false;

    // End made up syntax
}

I am transplanting to .NET from Delphi. This is pretty simple code to write to Delphi, but I'm not sure how to do it in C #.

+5
source share
3 answers

If you mean at least one of the flags:

return (combinations
     & (byte)( Combinations.Type1CategoryB | Combinations.Type2CategoryB)) != 0;

also - if you declare it as Combinations combinations(and not byte), you can leave(byte)

bool CheckForCategoryC(Combinations combinations)
{
    return (combinations & (Combinations.Type1CategoryB | Combinations.Type2CategoryB) ) != 0;
}

If you mean "exactly one of", I would just use switchor ifetc.

+16
source

"" , Umbrella CodePlex. . , , , , .

Enjoy.

+1

I think so, if I understood the question correctly

if (combinations == Combinations.Type1CategoryB | Combinations.Type2CategoryB)
-2
source

All Articles