When using C#, the compound assignment operators (+=
, *=
, etc.) are automatically created from the corresponding binary operator:
Compound assignment operators cannot be explicitly overloaded. However, when you overload a binary operator, the corresponding compound assignment operator, if any, is also implicitly overloaded. For example, += is evaluated using +, which can be overloaded. - C# Language Reference
If the binary operator is non-abelian, such as matrix multiplication, then the order of operands is important, for example A * B != B * A
. Therefore, if the compound assigment operators are used in the situations it is important to know in which order they treat the operands.
In what order do the automatically created compound assignment operators place the operands, e.g. is x *= y
equivalent to x = x * y
or to x = y * x
?
According to Assignment operators (C# reference) For a binary operator op, a compound assignment expression of the form
x op= y
is equivalent to
x = x op y
Except that x is only evaluated once.