Overloading + =, +, ==, and! = Operators

Possible duplicate:
Why should we define both == and! = In C #?

Why is overload + = possible only with overload +, but == and! = Are separately overloaded?
It seems to be inverted. + = overloading almost always allows you to write more efficiently, because there is no need to allocate memory for a new object. But I can’t come up with an example in which the == and! = Operators must be different in everything except inverting the result of Equals ().

+6
source share
1 answer

A similar question was asked before .

The biggest reason is that when overloading the == and != Operators, you do not need to return a boolean value. If you do not return a boolean, you cannot just invert the free operator. There are other possible reasons why they are overloaded separately, and you can view the answers to this question for these other reasons.

There is a valid reason why you cannot overload += , and therefore it is executed implicitly through the + operator. I have to do with the fact that you cannot override the assignment operator in C #, this is part of the standard language. += is the increment and destination, the last of which cannot be overloaded in C #.

+7
source

Source: https://habr.com/ru/post/895173/


All Articles