C#

C#

Made by DeepSource

Consider reusing existing instances of StringBuilder CS-P1020

Performance
Major

Instantiating 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.

Bad Practice

for (var i = lb; i < ub; i++)
{
    var sb = new StringBuilder();
    // ...
    // Append something.
    sb.Append(c);
}

Recommended

var sb = new StringBuilder();
for (var i = lb; i < ub; i++)
{
    // Clear the contents.
    sb.Clear();

    // ...
    // Append something.
    sb.Append(c);
}

Reference