Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET String concatenation (+ & +=) vs. StringBuilder

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?

like image 511
Matthew Layton Avatar asked Aug 02 '12 10:08

Matthew Layton


People also ask

What is the best way to concatenate strings in C#?

Concatenate String Using + Operator The simplest method of adding two strings in C# is using + or += operators.

What is string concat C#?

Concat Method is used to concatenate one or more instances of String or the String representations of the values of one or more instances of Object. It always returns a concatenated string. This method can be overloaded by passing different types and number of parameters to it.

How do you add two strings together?

Syntax: string new_string = string init + string add; This is the most easiest method for concatenation of two string. The + operator simply adds the two string and returns a concatenated string.


1 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.

like image 199
Hans Kesting Avatar answered Oct 04 '22 17:10

Hans Kesting