Simple way to overload compound assignment operator in C#?

c#, operator-overloading, overloading

Solution

You can't explicitly overload the compound assignment operators. You can however overload the main operator and the compiler expands it.

`x += 1` is purely syntactic sugar for `x = x + 1` and the latter is what it will be translated to. If you overload the `+` operator it will be called.

MSDN Operator Overloading Tutorial

public static Complex operator +(Complex c1, Complex c2) 
{
   return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

Problem

Does anyone have a very simple example of how to overload the compound assignment operator in C#?

Original source