First off, when defining a flags enum, each flag should represent a single bit in the enum:
enum X { a = 1, b = 2, c = 4, d = 8 }
This allows you to combine flags as well:
CandD = 12, //has both c and d flags set.
Or if you have a lot of them:
enum X {
a = 1 << 0,
b = 1 << 1,
c = 1 << 2,
d = 1 << 3,
...
CAndD = c | d
}
You can use a simple equality comparison to test if only certain flags are set.
public bool ContainsOnly(X value, X flags)
{
return value == flags;
}
public bool ContainsOnlyCandD(X value)
{
return value == (X.c | X.d);
}
public bool ContainsBothCandDButCouldContainOtherStuffAsWell(X value)
{
return (value & (X.c | X.d)) == (X.c | X.d);
}