在C#中+ =是什么意思 [英] what does += mean in C#

查看:118
本文介绍了在C#中+ =是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我遇到了
的代码行
str + = .....
我不清楚+ =在C#中的含义是我搜索不到的
很好的解释.
感谢

Hello I came across a line of code that is

str += .....
I am not clear what does += to means in c# i searched around couldnt come with
good explanation.
Thanks

推荐答案

您应该知道其他通用运算符也有类似的(赋值)运算符:
You should know that you have similar (assignment) operators for the other common operators:
x *= 8 => x = x * 8
x /= 8 => x = x / 8
x -= 8 => x = x - 8


这些是其他的:%=,& =,| =,^ =,<< =,>> =
当您有事件时,也可以使用此方法将事件处理程序添加到事件(或委托)中:


These are the other ones: %=, &=, |=, ^=, <<=, >>=
Also use this when you have events to add an event handler to an event (or delegate):

grid.PreviewKeyDown += GridPreviewKeyDown;


-=将从事件中删除事件处理程序.
注意:委托在事件上的执行方式相同,因此您可以对委托或事件进行多个分配,并且在执行委托或事件时,将执行所有分配. (感谢SAKryukov提醒我添加此内容)

仅供参考:在这种情况下,由于您无法搜索"+ =",因此很难找到信息.掌握Internet的搜索功能.


The -= will remove an event handler from the event.
Note: Delegates perform the same way at events, so you can have multiple assignments to either a delegate or event, and when the delegate or event is executed, all assignments will be executed. (thanks to SAKryukov for reminding me to add this)

FYI: Th is is a case where it is difficult to find informatin because you cannot search for "+=". A grip on search capabilities for the Internet.


对于字符串值,它将连接字符串.
对于整数值,它将把整数值相加.

基本上,它的目的是用于附加分配.
For string values it will concatenate the strings.
For integer values it will add up the integer values.

Basically its purpose is for addition assignment.
string strTestVal = "Hello!";
strTestVal += "world";


输出将是"Hello!world".
所以
也是一样


The output will be "Hello!world".
so it is the same with

strTestVal = strTestVal + "world";


例如整数值;


For example for integer values;

int total = 0;
for (int i = 0; i < 10; i++)
{
  total += i;
}


这里的总计将为"45".因此它是


here total will be "45".so it is

total = total + i;


您可以在此处 [


You can find more information about it here[^] in MSDN.

Good luck,
OI


在大多数情况下,"+ =是快捷方式:
In most cases, "+=" is a shortcut:
A += B;
C -= D;


等价于

A = A + B;
C = C - D;


任何二进制运算都可以代替"+"或-"来实现.

从以下意义上说,页面上的大多数答案都是错误的:它们说的是一些具体类型.实际上,此语法适用于具有定义的对应二进制操作的类型.这种澄清是必不可少的.考虑您定义某种类型(类或结构)并定义为运算符"+".这意味着您不必定义"+ ="(您也不能定义它),但是您可以假定它是根据您的"+"定义实现的.

请参阅:
http://msdn.microsoft.com/en-us/library/aa288467%28v = vs.71%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8edha89s.aspx [ ^ ],
http://blogs.msdn.com/b/ericlippert/archive/2007/05/14/why-are-overloaded-operators-always-static-in-c.aspx [


Any binary operation can come instead of "+" or "-".

Most of the answers on the page a wrong in the following sense: they say about some concrete types. In fact, this syntax works for type with defined correspondent binary operations. This clarification is essential. Consider you define some type (a class or structure) and defined to operator "+". It means that you don''t have to define "+=" (and you cannot define it), but you can assume it is implemented based on your "+" definition.

Please see:
http://msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx[^],
http://msdn.microsoft.com/en-us/library/8edha89s.aspx[^],
http://blogs.msdn.com/b/ericlippert/archive/2007/05/14/why-are-overloaded-operators-always-static-in-c.aspx[^].

Now, I said "in most cases", because working with delegate instances and event instances is different: you cannot use "+" or "-". The only valid operators are adding a handler to the invocation list or removing existing handler:

delegate void SomeOperationSignature(bool option);
//...
SomeOperationSignature someOperation;
//...
someOperation += (option) => { /* do something */ }
myButton.OnClick += (sender, eventArgs) => { /* do something else */ }



—SA



—SA


这篇关于在C#中+ =是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆