Most efficient way to concatenate strings?

Rico Mariani, the .NET Performance guru, had an article on this very subject. It’s not as simple as one might suspect. The basic advice is this:

If your pattern looks like:

x = f1(...) + f2(...) + f3(...) + f4(...)

that’s one concat and it’s zippy, StringBuilder probably won’t help.

If your pattern looks like:

if (...) x += f1(...)
if (...) x += f2(...)
if (...) x += f3(...)
if (...) x += f4(...)

then you probably want StringBuilder.

Yet another article to support this claim comes from Eric Lippert where he describes the optimizations performed on one line + concatenations in a detailed manner.

Leave a Comment