6

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 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

The reason is simple:

because

string result = a + b;

is more efficient than

var sb = new StringBuilder(a, a.Length + b.Length);
sb.Append(b);
string result = sb.ToString();

StringBuilders only make sense when lots of concatenations are happening, usually inside a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.