Aller au contenu

String builder optimization (ARCH608)

Ce contenu n’est pas encore disponible dans votre langue.

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.

var sb = new StringBuilder();
foreach (var line in lines) // thousands of lines
sb.AppendLine(line);
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.