String builder optimization (ARCH608)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Key dotnet.performance.string-builder-optimization · Severity info · Category performance
Providing an approximate initial capacity prevents StringBuilder from repeatedly growing and reallocating its internal buffer.
Incorrect
Section titled “Incorrect”var sb = new StringBuilder();foreach (var line in lines) // thousands of lines sb.AppendLine(line);Correct
Section titled “Correct”var sb = new StringBuilder(lines.Count * 64);foreach (var line in lines) sb.AppendLine(line);Construct StringBuilder with an initial capacity when the expected size is known.