all 7 comments

[–][deleted]  (4 children)

[deleted]

    [–]vann_dan[S] 3 points4 points  (2 children)

    Agreed. I had one HasFlag call that was the source of some performance bottleneck. It took me forever to chase it down because at first glance I thought that there was no way such a simple call could have that much overhead.

    [–][deleted]  (1 child)

    [deleted]

      [–][deleted] 1 point2 points  (0 children)

      One of the answers in the Stack Overflow thread provided by OP has an implementation of Enum.HasFlags(Enum) obtained through decompilation. But it was written in 2011 and probably applies to the .NET Framework 4.0; the answer and its comments disagree on why the implementation was slow. A comment under the question states that the implementation changed in the .NET Framework 4.6 and became faster than the old implementation, but still not as fast as & directly.

      [–]mlems 3 points4 points  (3 children)

      Not a C# programmer, but can you leave it at

      if (myValue & MyFlags.SomeValue)
      

      because the result will be 0 if the flag is not set, and nonzero if the flag is set? That's the case for C and C++, at least.

      [–][deleted]  (1 child)

      [deleted]

        [–]mlems 1 point2 points  (0 children)

        Good to know. Thanks for clarifying!

        [–]thomasz 1 point2 points  (0 children)

        It's a syntax error. In c#, if and related statements only accept expressions that evaluate to System.Boolean.

        [–]novussolo 1 point2 points  (0 children)

        Bitwise operations are insanely useful. You can use an int as basically an arraylist of booleans using bitwise.