Skip to main content
ai-slop/csharp-string-concat-in-loopmanual fix

String concatenation in a loop.

`result += "..."` inside a loop. Strings are immutable, so each iteration allocates a new string and copies the old one: quadratic work for a linear job.

Flagged shape and clean shape.

Each example shows the exact code shape the rule is looking for, then the smallest version that keeps the intent without hiding risk or adding noise.

csharp-string-concat-in-loop.cs
✗ Flagged
var csv = "";
foreach (var row in rows)
{
    csv += row.Id + "," + row.Name + "\n";
}
return csv;
Why this trips

Deterministic brace-depth scan: tracks `for`, `foreach`, `while`, and `do` bodies and flags `name += ...` statements inside them that include a string literal.

csharp-string-concat-in-loop.cs
✓ Clean
var csv = new StringBuilder();
foreach (var row in rows)
{
    csv.Append(row.Id).Append(',').Append(row.Name).Append('\n');
}
return csv.ToString();
What changed

Append to a `StringBuilder` inside the loop and call `.ToString()` once afterwards, or use `string.Join` over the sequence.

Agents build output the way it reads, appending piece by piece, and do not switch to a builder when the append moves inside a loop.

How this rule is justified.

Rule id

ai-slop/csharp-string-concat-in-loop

Enforcing engine

ai-slop

Detector strategy

Deterministic brace-depth scan: tracks `for`, `foreach`, `while`, and `do` bodies and flags `name += ...` statements inside them that include a string literal.

Legitimate code that is NOT flagged

`+=` lines without a string literal (numeric accumulators, `s += name`), concatenation outside loop bodies, and loops whose body has no braces are not flagged.

Evidence

Part of aislop's C# rule set. First-class C# support, including C#-specific AI-slop rules, arrived in aislop v0.14.1.