Click here to Skip to main content
15,900,475 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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
Posted

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;

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.
 
Share this answer
 
v4
Comments
ProEnggSoft 26-Mar-12 20:39pm    
Nice additional info. +5
Clifford Nelson 26-Mar-12 20:41pm    
Thanks.

Clifford
Sergey Alexandrovich Kryukov 26-Mar-12 21:12pm    
Not 100% exact: +=, -= not just for events but for delegate instances as well.
Also, your => is confusing, because this is the lambda operator.
You should have mentioned user-defined operators.
--SA
Clifford Nelson 26-Mar-12 22:27pm    
True, should have included delegates. I realise that what I was using "=>" is also used for lambda expressions, but the only other option would have been wordy or required putting in a special symbol.

I have not found User Defined operators are very common. Hopefully when somebody creates a User Defined assignment operator they will not do something unexpected. There is that possibility, in which case the programmer has to be aware that somebody can redefine operators. I have only occationally defined operators in a class.
Sergey Alexandrovich Kryukov 27-Mar-12 23:11pm    
Common or not, it does not matter. The idea of += vs. + is that += applicability formally depends only on one factor: availability of definition of "+". This is all what matters.
--SA
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.
C#
string strTestVal = "Hello!";
strTestVal += "world";

The output will be "Hello!world".
so it is the same with
C#
strTestVal = strTestVal + "world";

For example for integer values;
C#
int total = 0;
for (int i = 0; i < 10; i++)
{
  total += i;
}

here total will be "45".so it is
C#
total = total + i;

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

Good luck,
OI
 
Share this answer
 
v3
Comments
ProEnggSoft 26-Mar-12 20:36pm    
Nice explanation. +5
In most cases, "+=" is a shortcut:
C#
A += B;
C -= D;

are equivalent to
C#
A = A + B;
C = C - D;

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:

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


—SA
 
Share this answer
 
+= is shortcut for addition or contentation


C#
string s = "Foo";
s += "Bar";

int x = 0;
x += 2;


would be equivalent to

C#
string s = "Foo";
s = s + "Bar";

int x = 0;
x = x + 2;
 
Share this answer
 
Comments
ProEnggSoft 26-Mar-12 20:37pm    
Succinct and to the point. +5
Means just to append the string in the right hand of the operator to the content already in the variable in the left side of the operator.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900