Use StringBuilder when you’re concatenating strings in a very long loop or in a loop within an unknown size – especially if you don’t know for sure (at compile time) how many iterations you’ll make through the loop. For example, reading a file a character at a time, building up a string as you go.
Use String Concatenation operator when you can specify everything which needs to be concatenated in one statement. (If you have an array of things to concatenate, consider calling String.Concat explicitly – or String.Join if you need a delimiter.), avoid using the (+=) or the normal (+) for strings concatenation.
If you need the intermediate results of the concatenation for something other than feeding the next iteration of concatenation, StringBuilder isn’t going to help you. For instance, if you build up a full name from a first name and a last name, and then add a third piece of information (the nickname, maybe) to the end, you’ll only benefit from using StringBuilder if you don’t need the (first name + last name) string for other purpose (as we do in the example which creates a Person object).
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.