StringBuilder
is better for building up a string from many non-constant values.
If you're building up a string from a lot of constant values, such as multiple lines of values in an HTML or XML document or other chunks of text, you can get away with just appending to the same string, because almost all compilers do "constant folding", a process of reducing the parse tree when you have a bunch of constant manipulation (it's also used when you write something like int minutesPerYear = 24 * 365 * 60
). And for simple cases with non-constant values appended to each other, the .NET compiler will reduce your code to something similar to what StringBuilder
does.
But when your append can't be reduced to something simpler by the compiler, you'll want a StringBuilder
. As fizch points out, that's more likely to happen inside of a loop.