Skip to content

String concat in loop (ARCH601)

Key dotnet.performance.string-concat-in-loop · Severity warning · Category performance

Strings are immutable, so each concatenation allocates a new object; in a loop this creates many temporary allocations.

var result = "";
foreach (var item in items)
{
result += item.Name + ", ";
}
var sb = new StringBuilder();
foreach (var item in items)
{
sb.Append(item.Name).Append(", ");
}
var result = sb.ToString();

Accumulate with StringBuilder.