String Performance: Concatenating Strings

Updated JANUARY 2024

There are various methods to concatenate strings in .NET. The most common approach, often employed by developers, is as follows:

string result = "David" + " " + "McCarter";

Before we delve into the remaining aspects of this section, it’s essential to emphasize that strings in .NET are immutable. Here is the definition of immutability:

In programming, immutability refers to the characteristic of an object or data structure that, once created, cannot be modified or changed. Immutable objects are not subject to alteration after their initial creation. Any operation performed on an immutable object results in the creation of a new object rather than modifying the existing one.

Due to this immutability, combining multiple strings could lead to performance and memory issues. I frequently observe developers employing such practices in their code, even when constructing large SQL queries (which could also pose security concerns).
To assess performance, I have conducted a benchmark by iterating through a string array to concatenate the strings. The following code was used for the report.

var result = string.Empty;

Example #1
string.Concat(this.stringArray);

Example #2
foreach (var item in this._stringArray)
{
    result = string.Join(ControlChars.Space, item);
}

Example #3
foreach (string _stringArray in this._stringArray)
{
    result = result + _stringArray + ControlChars.Space;
}
or
foreach (string _stringArray in this._stringArray)
{
    result += _stringArray + ControlChars.Space;
}

One limitation of using Concat() is that it does not support the addition of a separator.

Benchmark Results

Let’s examine the performance of these three methods for combining strings from a string array with varying word counts.

Considering the results, Concat() and Join() stand out as the fastest method when dealing with an array of strings. I strongly advise against using += or + for string concatenation, as it leads to a significant performance decline and larger allocations.

Allocations

There is a substantial difference in the memory allocations observed in these four benchmark tests:

  • Concat() & Join(): 504 – 61,464 bytes
  • + and +=: 4,736 – 67,190,801 bytes

You can also enhance performance by utilizing StringBuilder!

Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

If you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave

© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

2 thoughts on “String Performance: Concatenating Strings

  1. Curious that you did not benchmark StringBuilder as it is usually the recommended approach for multiple appends/concatentations…..

Leave a reply to thecpuwizard Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.