I am reading a book called .NET Gotchas (well worth a read IMO), which illustrates the performance differences between String and StringBuilder, and it raised a question which I felt could not go unanswered! Whilst I do not know the internals of either class (without looking at reflected versions of these classes), I was wondering; since operators in .NET are overloadable, why did Microsoft not implement the String class to use StringBuilder internally and overload the concatenation operators to simply call .Append() in StringBuilder? I'm guessing there are some underlying reasons as to why this is not the case, and if so, why?
2 Answers
The problem is not so much that string concatenation is slow, it is more that repeated concatenation creates a lot of intermediate strings that need to be allocated and later garbage collected.
EDIT
Note that mystring += "a" doesn't simple add "a" to the previous string. It creates a new string for the combination and points "mystring" to it, thereby discarding the previous value (if there are no more references to it).
END EDIT
A series of
string mystring = "something";
mystring += "something else";
mystring = mystring + "third";
will perform slower if you do each separate line as a StringBuilder Append followed by a .ToString() to get the result back in a string. You will only get a performance benefit if you use a single StringBuilder, Append() to it repeatedly and do a .ToString() at the very end.
StringBuilder sb = new StringBuilder();
sb.Append("something");
sb.Append("something else");
sb.Append("third");
string mystring = sb.ToString();
And then a StringBuilder has it's own overhead so that it will not benefit you if you have a small number of string-parts to join.
Note that the compiler optimizes away a concatenation in a single statement:
string mystring = "something" + "something else" + "third";
is fastest.