Why this matters
Creating a `StringBuilder` without calling `ToString()` results in unused objects, wasting memory and reducing performance.
Ensure that `StringBuilder` instances are converted to strings using `ToString()` before use to prevent memory wastage.
Creating a `StringBuilder` without calling `ToString()` results in unused objects, wasting memory and reducing performance.
Side-by-side examples engineers can pattern-match during review.
public void DoSomething(List<string> strings) {
var sb = new StringBuilder(); // Noncompliant
sb.Append("Got: ");
foreach(var str in strings) {
sb.Append(str).Append(", ");
// ...
}
}public void DoSomething(List<string> strings) {
foreach(var str in strings) {
// ...
}
}public void DoSomething(List<string> strings) {
var sb = new StringBuilder(); // Noncompliant
sb.Append("Got: ");
foreach(var str in strings) {
sb.Append(str).Append(", ");
// ...
}
}public void DoSomething(List<string> strings) {
foreach(var str in strings) {
// ...
}
}From the same buckets as this rule.