Performance Wars — String Concatenation — C#
The concatenation of strings is a common and useful task. So the main question here is: What’s the most efficient way to do it? Let’s find out!
All the tests were done with the BenchmarkDotNet NuGet package and using the .NET 5.0 version. You can check my GitHub repository to see the project details.
The main idea for these tests was to have a list of numbers and concatenate them in a string, just that. The used list has 3 different sizes: 10, 100, and 1000 to understand how the size affects the time and allocated memory.
Results
Size 10
For a small list with 10 elements, there is not a huge difference between the approaches. However, there is a visible gap in allocated memory for the plus operator compared with the others. Let’s look at the results for a list of 100.
Size 100
We can see a difference using a list with a few more elements. The plus operator keeps the…