-11

I have some code that contains the operator '+='.

Specifically, the code reads as follows:

foreach (KeyValuePair<String, String> row in connectionOpts)
            {
                str += String.Format("{0}={1}; ", row.Key, row.Value);
            }

What function does this operator perform?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
MX26
  • 15
  • 4
  • 2
    What do you mean by _What function does this operator perform_? `x += y` is equal to `x = x + y`. Take a look [+= Operator (C# Reference)](http://msdn.microsoft.com/en-us/library/sa7629ew.aspx) – Soner Gönül Sep 13 '14 at 14:14
  • 1
    Also, have a look at using a `StringBuilder` instead instead of concatenating the strings (strings are immutable so you are allocating a lot of unneeded memory). – flindeberg Sep 13 '14 at 16:29

2 Answers2

2

It is an assignment operator. It adds right operand to the left operand and assigns the result to left operand.

You may want read few tutorials, so you can gain better understanding of c# fundamentals.

StaWho
  • 2,488
  • 17
  • 24
0

It adds two strings(or ints etc) together.

String a = "Hello";
a += " World";

String a now = "Hello World"

int i = 0;
i += 2;

int I now = 2

Andy
  • 49,085
  • 60
  • 166
  • 233
marsh
  • 2,592
  • 5
  • 29
  • 53