StringBuilder
CS-P1020Instantiating a new instance of StringBuilder
requires the initialization of the underneath buffer that holds the string
contents. Creating a new instance in a loop may have an adverse performance impact. Instead, it is recommended that you initialize StringBuilder
outside the loop and reuse it within the loop by clearing it when required.
for (var i = lb; i < ub; i++)
{
var sb = new StringBuilder();
// ...
// Append something.
sb.Append(c);
}
var sb = new StringBuilder();
for (var i = lb; i < ub; i++)
{
// Clear the contents.
sb.Clear();
// ...
// Append something.
sb.Append(c);
}