Possible Duplicate:
Simple way to overload compound assignment operator in C#?
I was playing around with events and thought events are weird. Why couldnt i just implement them in a generic class. So i tried to and found that i CANT OVERLOAD +=. From the language specs found here
The overloadable unary operators are:
+ - ! ~ ++ -- true false
The overloadable binary operators are:
+ - * / % & | ^ << >> == != > < >= <=
+= is not listed. Now, before you say theres no reason to overload += i'd like to bring up the fact C# has events which uses the += operator and the fact i tried to implement an event for fun and wanted to use the += operator. Now, i have a feeling someone will say thats why events exist, because it is the only reason. However i want to bring up you can use += with the TimeSpan struct. Go try it, var ts= new TimeSpan(); ts += ts;
will compile and run.
I looked at TimeSpan definition and i have no idea how it is allowing it. I saw a public static TimeSpan operator +(TimeSpan t);
which looked suspicious but then i realize its for something like var name = +ts;
like how you can do var name = -ts;
for negation.
So my question is, how do i use the += for my struct or class. It appears to be supported i just cant seem to find the documentation on it.