Anti-Pattern: String Concatenation in Loops
Descriptionā
Using the += operator to concatenate strings inside a loop. Since strings are immutable in C#, this creates a new string object for every iteration, copying the old content plus the new content.
Impactā
- Performance: O(N²) complexity for memory allocation and copying.
- Memory: Heavy pressure on the Garbage Collector (GC) due to thousands of temporary objects.
Detectionā
Loops containing str += ... or str = str + ....
// ā BAD: Creates N string objects
string summary = "";
foreach (var item in items) {
summary += item.Name + "<br/>";
}
Refactoringā
Use StringBuilder for loop-based concatenation.
// ā
GOOD: Allocates a buffer and expands only when needed
StringBuilder sb = new StringBuilder();
foreach (var item in items) {
sb.Append(item.Name).Append("<br/>");
}
string summary = sb.ToString();
2026 Audit Findingsā
- Occurrences: > 2,000 candidates.
- Severity: Medium (Critical in Hot Paths).
- Key Locations:
TravelTicketProposalOMGT.cs(Manual HTML building)OrderTravelProductOMGT.cs(Log text construction)