Just for fun, I took benchmarks using BenchmarkDotNet to see what’s the fastest out of common ways used to concatenate a small number of strings together in .NET 8.
Even though it’s not commonly used I benchmarked String.Create too (not shown but available in code which is linked to below) and it is super fast but also super nasty looking.
Click on the image for a larger view in a new window …
String.Join kicks ass, but I mostly use interpolation as it’s very readable.
Obviously these benchmarks just correspond to the specific case above and when run on my machine. The important thing to note is that you when you are in a performance sensitive context and micro-optimization matters you need to benchmark for your own specific scenario.
What do you think?
Any surprises here?
Resources
Benchmark code if you’d like to recreate the benchmarks ->
.NET 8 simple string concatenation benchmarks (github.com)
SharpLab link if you wish to have a look at the IL for the different concat approaches.
Interestingly we can see that + is just a wrapper around concat …