String formatting
If we don't know the final size of the resultant string, then using a StringBuilder class is unlikely to generate a buffer that fits the result size exactly. We will either end up with a buffer that's too large (wasted space) or, worse, a buffer that's too small, which must keep expanding as we generate the complete string. In this scenario, it might be best to use one of the various string class formatting methods.
There are three string class methods available for generating strings: string.Format(), string.Join(), and string.Concat(). Each operates slightly differently, but the overall output is the same. A new string object is allocated, containing the contents of the string objects we pass into them, and it is all done ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access