why C# not allow operator overloading?

前端 未结 5 1411
醉酒成梦
醉酒成梦 2021-01-17 06:43

why c# not allowed operator overloading while C++ do?

I got this error, when trying to overload.

Overloadable binary operator expected

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 07:31

    You can overload operators in C#:

    • MSDN Tutorial

    Before you decide to use operator overloading, please read at the very least, the following:

    • Criticisms of Operator Overloading

    EDIT in response to op's edit:

    The += is a non-overloadable operator. However, it resolves to +, and + is overloadable. Have a look here for which operators you can and can not overload.

    Operators that can be overloaded:

    +, -, !, ~, ++, --, true, false

    +, -, *, /, %, &, |, ^, <<, >>

    ==, !=, <, >, <=, >= (The comparison operators can be overloaded but see note in link)

提交回复
热议问题