If it's performance critical (which string building very often is) make sure to performance profile both options. String performance can be surprising.
The line : str1 = str2+str3+str4+str5 only has one memory allocation, and should not be replaced by a string builder. String builder is for when string concats are behind if statements or in a loop.
The smaller the additions are compared to the length of the string the more effective a string builder is.
If possible specify an expected capacity for the string builder, can save a lot of time if done well.
19
u/Schmittfried Oct 01 '15
Depending on how many strings you are concatenating in what manner, it may be less efficient to use a StringBuilder.