为什么不能我重载+ =运算符在C#中?但我仍然可以使用它? [英] Why cant i overload the += operator in C#? But i still can use it?
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/2869245/simple-way-to-overload-compound-assignment-operator-in-c">Simple办法在C#中重载复合赋值操作符?
我用事件玩弄,并认为事件是怪异。为什么想不出我只是实现他们在一个泛型类。于是,我就发现,我不能过载+ =。 从这里发现语言规范
I was playing around with events and thought events are weird. Why couldnt i just implement them in a generic class. So i tried to and found that i CANT OVERLOAD +=. From the language specs found here
The overloadable unary operators are:
+ - ! ~ ++ -- true false
The overloadable binary operators are:
+ - * / % & | ^ << >> == != > < >= <=
+ =没有列出。现在,在你说世界上没有理由超载+ =我想弹出事实上C#有事件,它使用+ =运算符和事实,我试图实现事件的乐趣和希望使用+ =运算符。现在,我有一种感觉,有人会说,这就是为什么事件的存在,因为它是唯一的原因。不过,我想提出你可以使用+ =用的时间跨度结构。去试试吧, VAR TS =新的时间跨度(); TS + = TS;
编译和运行
我看了看时间跨度定义,我不知道它是如何允许它。我看到了一个公共静态时间跨度运营商+(时间跨度T);
这看起来很奇怪,但是后来我意识到它的东西,像 VAR名称= + TS;
喜欢你如何能做到 VAR名称= -TS;
为否定
I looked at TimeSpan definition and i have no idea how it is allowing it. I saw a public static TimeSpan operator +(TimeSpan t);
which looked suspicious but then i realize its for something like var name = +ts;
like how you can do var name = -ts;
for negation.
所以我的问题是,我该如何使用+ =我的结构或类。这似乎是支持我只是不能似乎找到它的文档。
So my question is, how do i use the += for my struct or class. It appears to be supported i just cant seem to find the documentation on it.
推荐答案
事件不超载 + =
和 = -
运营商。这只是很难codeD在C#编译器它转换成的添加/删除事件的方法。
Events don't overload the +=
and -=
operator. This is just hardcoded in the C# compiler which translates it into the add/remove methods of that event.
您只能超负荷 +
和 -
然后间接超载 + =
和 = -
。如果你重载 +
然后自动 X + = Y
过载到 X = X + Y
。因此,对于你的类型,你不需要投入任何额外的工作。只要超载 +
,你会得到 + =
免费。当然,这需要在左侧的参数和结果类型是兼容的,但是这是通常情况下。
You can only overload +
and -
which then indirectly overload +=
and -=
. If you overload +
then automatically x += y
becomes overloaded to x = x + y
. So for your type you don't need to put in any extra work. Just overload +
and you'll get +=
for free. This of course requires the left side argument and the result type being compatible, but that's usually the case.
这是你不能重载的原因 + =
分别是最有可能的,它创建奇怪的语义引用类型。如果 + =
修改的左侧则现有实例以下code会表现得出奇:
The reason that you can't overload +=
separately is most likely that it creates strange semantics for reference types. If +=
modified the existing instance of the left side then the following code would behave strangely:
MyClass x=new MyClass();
MyClass y=x;
x+=SomeThing;
y has changed now too
在另一方面与 + =
意 X = X + Y
这code创建一个新的例如,它分配到x。而原始实例保持不变y中;
On the other hand with +=
meaning x = x+y
this code creates a new instance which it assigns to x. And the original instance remains unmodified in y;
在C ++中,另一方面,可以安全地指定 + =
单独的过载,因为你可以重载赋值和拷贝构造函数在同一时间和使用,要得到正确的语义。
In C++ on the other hand it is possible to safely specify a separate overload for +=
because you can overload the assignment and copy-constructor at the same time and use that to get the correct semantics.
这篇关于为什么不能我重载+ =运算符在C#中?但我仍然可以使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!