The problem is not so much that string concatenation is slow, but that repeated concatenation creates a lot of intermediate lines that need to be allocated and garbage collected later.
EDIT
Note that it mystring += "a"doesnβt just add βaβ to the previous line. He creates a new line for the combination and points to it "mystring", thereby discarding the previous value (if there are no more links on it).
End edit
Row
string mystring = "something";
mystring += "something else";
mystring = mystring + "third";
will work slower if you execute each individual line as adding StringBuilder and then .ToString () to return the result to the string. You will only get performance gains if you use one StringBuilder, Append () for it several times and do .ToString () at the very end.
StringBuilder sb = new StringBuilder();
sb.Append("something");
sb.Append("something else");
sb.Append("third");
string mystring = sb.ToString();
StringBuilder , , .
, :
string mystring = "something" + "something else" + "third";
.