Q:

Parsing an enum with the Flags attrubute not giving expected value

My enum:
        [Flags]
        public enum EqualityOperator
        {
            Equal,
            NotEqual,
            LessThan,
            LessThanOrEqual,
            GreaterThan,
            GreaterThanOrEqual,
            Like,
            NotLike,
            In,
            NotIn
        }

My code for parsing it:
var operatorVal = (EqualityOperator)Enum.Parse(typeof (EqualityOperator), filterInfo[3]);

When I debug, I can see that filterInfo[3] is "Like"
However, operatorVal comes out as "LessThan | GreaterThan"
What am I missing? Can you not parse enums with the Flags attribute?

A:

You need to also specify the values:
[Flags]
public enum EqualityOperator {
    Equal = 0,
    NotEqual = 1,
    LessThan = 2,
    LessThanOrEqual = 4,
    GreaterThan = 8,
    GreaterThanOrEqual = 16,
    Like = 32,
    NotLike = 64,
    In = 128,
    NotIn = 256
 }

The reason that Like is parsing as LessThan | GreaterThan is because as you've defined it, LessThan has value 2 and GreaterThan has value 4. If you take the bitwise-or of these, you end up with LessThan | GreaterThan = 6. But look, Like has the value 6 as you've defined your enum! So, it did parse "correctly".
I'll be frank with you though, I don't see the point of marking this enum with Flags though. The point of Flags is so that you can do bitwise operations on the enum values. Why do you think you need to do bitwise operations on the values of this enum? 

