0

My question is fairly simple. Take this enum:

public enum Foo
{
   Foo = 1,
   Bar = 2,
   FooBar = 4
}

Now when applying the AND/OR bitwise operator with a zero on one side I do not need a cast and everything works fine e.g.

Foo f = Foo.Bar & 0;  // works perfectly fine and compiles 

As soon as I change the natural number to any other value then 0, I need to cast the number.

Foo f = Foo.Bar & 1;  // doesn't work nor compile

The error would be the following:

Operator '&' cannot be applied to operands of type 'Foo' and 'int'

Now how comes, that 0 does not to be casted where as any number as to be casted to the given enum?

Twenty
  • 4,232
  • 3
  • 24
  • 56
  • @Jawad What array? – Tanveer Badar Jan 03 '20 at 04:50
  • @Jawad Wait, I can't follow, the AND operator is creating arrays?? – Twenty Jan 03 '20 at 04:50
  • Does this answer your question? [What does the \[Flags\] Enum Attribute mean in C#?](https://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c) – Jawad Jan 03 '20 at 04:53
  • 1
    Does this answer your question? [Why switch for enum accepts implicit conversion to 0 but no for any other integer?](https://stackoverflow.com/questions/14950750/why-switch-for-enum-accepts-implicit-conversion-to-0-but-no-for-any-other-intege) – voidengine Jan 03 '20 at 04:54
  • @voidengine I actually didn't found that after searching, thanks a lot! – Twenty Jan 03 '20 at 04:56

0 Answers0