Fork me on GitHub
TypeName StringBuilderInLoopAnalyzer
Check Id CC0039
Category Performance
Severity Warning

Cause

Do not concatenate a string on a loop. It will alocate a lot of memory. Use a StringBuilder instead. It will will require less allocation, less garbage collector work, less CPU cycles, and less overall time.

Example

var values = "";
foreach (var item in Enum.GetValues(typeof(LoaderOptimization)))
{
    values += item.ToString();
}

Code fix

A code fix will be presented to you that will transform the code, it will use StringBuilder to concatenate the string.

var values = "";
var builder = new StringBuilder();
builder.Append(values);
foreach (var item in Enum.GetValues(typeof(LoaderOptimization)))
{
    builder.Append(item.ToString());
}
values = builder.ToString();

Code fix

Related rules

TBD

See also

TBD