Ok, after another debate on how strings should be used, I spent a few minutes performing quick benchmark tests on different string usage scenarios and these are my findings.
- Try not to use String.Format() to concatenate strings, its a huge performance hit.
- Refrain from concatenating a string object to another string object/literal, especially in a loop. This was the slowest of the bunch.
- Always and I mean always use a StringBuilder when concatenating a huge pile of strings, this method was extremely fast - especially in a loop. It was about 300% faster than the other methods.
- New. Please feel free to use string literals anytime. It turns out that the Microsoft .Net interning system works as planned and extremely well. Some developers believe that a new object is created everything a literal is created, however, that is not true. The .Net string intern table(intern pool) maintains a string instance for every string and simply reuses/returns a reference if the same string sequence is detected. Read more about String.Intern().
- Obvious - Don't use a StringBuilder to concatenate a small set of strings.