String concat in loop (ARCH601)
Esta página aún no está disponible en tu idioma.
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.
Incorrect
Section titled “Incorrect”var result = "";foreach (var item in items){ result += item.Name + ", ";}Correct
Section titled “Correct”var sb = new StringBuilder();foreach (var item in items){ sb.Append(item.Name).Append(", ");}var result = sb.ToString();Accumulate with StringBuilder.