For simple string concatenations use the +
approach. It is clearer for simple things that don't require a format.
For more complex strings that have a certain format and where it is useful to retain the structure of the entire string and provide a placeholder for the input, use String.Format
.
And yes, there is an overhead. String.Format
uses a StringBuilder underneath the covers. Simple string concatenations will be much quicker in those scenarios. A couple of benchmarks and blog posts on this topic can be found quite easily. Of course it all depends on your usage. If small string concats are occurring in a loop then repeated usage of String.Format
will likely be more noticeable than a straightforward +
concat. If you are building up a large string in a loop then the classic example is to prefer StringBuilder
and related questions on concat versus StringBuilder can be found on SO.
EDIT: To clarify, this serves little purpose: String.Format("{0}{1}", a, b)
since there is not much formatting. It's simply a + b
. Unfortunately I've come across such examples in production code and as soon as I see String.Format I expect to see something that needs to be structured a certain way, not a straightforward concat.
OTOH, consider this phone number: "(" + area + ") " + number + " x" + extension
- there's too much going on and it's not easy to modify. In this case a String.Format is preferable: String.Format("({0}) {1} x{2}", area, number, extension)
. This is still a trivial example but you get the idea.