Why does addition require a cast but subtraction works without a cast? See the code below to understand what I am asking
public enum Stuff
{
A = 1,
The default values for enums are 0,1,2... so in this case the difference between two enums will create another enum (compare them first to make sure you are subtracting the smaller from the larger).
Half the time addition will create a value too high to be a valid enum.
Good question - I was surprised that the first and third lines worked.
However, they are supported in the C# language specification - in section 7.8.4, it talks about enumeration addition:
Every enumeration type implicitly provides the following pre-defined operators, where E is the enum type and U is the underlying type of E:
E operator +(E x, U y) E operator +(U x, E y)
At runtime, these operators are ealuated exactly as (E)((U)x + (U)y)
And in section 7.8.5:
Every enumeration type implicitly provides the following predefined operator, where E is the enum type and U is the underlying type of E:
U operator -(E x, E y)
This operator is evaluated exactly as
(U)((U)x - (U)y))
. In other words, the operator computes the difference between the ordinal values ofx
andy
, and the type of the result is the underlying type of the enumeration.E operator -(E x, U y);
This operator is evaluated exactly as
(E)((U)x - y)
. In other words, the operator subtracts a value from the underlying type of the enumeration, yielding a value of the enumeration.
So that's why the compiler behaves like that - because it's what the C# spec says to do :)
I wasn't aware that any of these operators exist, and I've never knowingly seen them used. I suspect the reasons for their existence are buried somewhere in the language design meeting notes that Eric Lippert occasionally dives into - but I also wouldn't be surprised if they were regretted as adding features for little benefit. Then again, maybe they're really useful in some situations :)
The problem is that "+" in this context is not like a plus between the values of enumeration. It is important to understand that + is operator and there are no rules defined how to apply it to operands ( Stuff.A and Stuff.B)