String Concatenation Performance


« Back

How do I concatenate strings in C#?

This is a pretty common question amongst beginners. Building a string is a basic function for any programming language, but how that string is built can matter greatly to a program’s performance. The typical wisdom in C# for this question is to use the System.Text.StringBuilder class. Using a class instead of using the basic concatenation features of the language has overhead though.

So the major question is: At what point does it make sense to incur the overhead of instantiating the StringBuilder class?

There were two options that were tested:

  • Use the StringBuilder class
  • Concatenation with the + operator


* BenchmarkDotNet was used to collect the above metrics.

The results showed that the point at which instantiating the StringBuilder class merits the overhead is 10 concatenations. All of which means, small occurences of concatenation using the + operator is actually more efficient. If a lot of concatenations (i.e. possibly more than 10) are going to occur, it is best to centralize that logic and utilize a single instance of the StringBuilder class.