开发者

why C# not allow operator overloading?

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

I got this error, when trying to overload.

Ov开发者_如何学编程erloadable binary operator expected

My sample code looks like,

public static MyClass operator +=(MyClass obj1, MyClass obj2)
        {
           ...
        }


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)


Operator overloading, C# allows. (Yoda version)


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.


c# does allow operator overloading. MSDN tutorial here.


You can overload operators in C#. Operator Overloading Tutorial

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜