why C# not allow operator overloading?

前端 未结 5 1384
醉酒成梦
醉酒成梦 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:24

    Operator overloading, C# allows. (Yoda version)

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2021-01-17 07:36

    It does allow operator overloading..

    that said, it's generally recommended not to override operators since it changes the semantics of your code and therefore makes it less maintainable by others.

    0 讨论(0)
  • 2021-01-17 07:40

    c# does allow operator overloading. MSDN tutorial here.

    0 讨论(0)
  • 2021-01-17 07:44

    You can overload operators in C#. Operator Overloading Tutorial

    0 讨论(0)
提交回复
热议问题