Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading +=, +, ==, and != operators [duplicate]

Possible Duplicate:
Why must we define both == and != in C#?

Why is overloading of += possible only by overloading of +, but == and != are separately overloaded?
It seems it should be inverted.
+= overloading is nearly always possible to write more effective because it's unnecessary to allocate memory for new object. But I can't invent an example in which operators == and != should be different in something except inverting the result Equals().

like image 630
Nelson Tatius Avatar asked Aug 16 '11 17:08

Nelson Tatius


1 Answers

A similar question has been asked before.

The biggest reason is that when you overload the == and != operators, you don't have to return a boolean. If you're not returning a boolean, you can't just invert the complimentary operator. There are other possible reasons they are separately overloaded, you can view the answers to that question for those other reasons.

There is a valid reason you can't overload += and therefore it is done implicitly via the + operator. I has to do with the fact that you can't override the assignment operator in C#, it's part of the language standard. The += is increment and assign, the latter of which you can't overload in C#.

like image 102
Christopher Currens Avatar answered Oct 23 '22 06:10

Christopher Currens