Imagine you have this enum definition and a variable of same type:
[Flags] public enum Tubo { One, Two } ... Tubo tubo = Tubo.One;
Now, how do you check if a variable of type Tubo contains a flag, i.e. Tubo.One? Simple, like this:
bool b = (tubo & Tubo.One) == Tubo.One;
But do you really like this notation? I mean it is a lot of typing and the expression is not clear at first sight. That's why I checked out if extension methods might help. So I created this experimental method:
public static bool Contains<T>(this T list, T flag) where T : struct { return (Convert.ToInt32(list) & Convert.ToInt32(flag)) == Convert.ToInt32(flag); }
And the new test would look like this:
bool b = tubo.Contains(Tubo.One);
Isn't this notation much more readable and easy to type? Sure. But there are two drawbacks in here:
public static bool Contains<T>(this T list, T flag) where T : struct { return (int)list & (int)flag == (int)flag; }
But if the code above worked one would get a decent performance, not much slower than doing a simple bitwise and operation.
Unfortunately there is no better solution AFAIK. It would certainly help if we could restrict generic type T to enum (where T: enum).