C# accepts this:
this.MyMethod(enum.Value1 | enum.Value2);
and this:
this.MyMethod(enum.Value1 & enum.Value2);
Whats the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you do
|, you select both. When you do&, you only what overlaps.Please note that these operators only make sense when you apply the
[Flags]attribute to your enum. See http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx for a complete explanation on this attribute.As an example, the following enum:
And a few test cases:
Here we test that
testValueoverlaps withValue1And2(i.e. is part of):Here we test whether
testValueis exactly equal toValue1And2. This is of course not true:Here we test whether the combination of
testValueandValue2is exactly equal toValue1And2: