String concatenation in loop¶
ID: cs/string-concatenation-in-loop
Kind: problem
Security severity: 
Severity: recommendation
Precision: very-high
Tags:
   - efficiency
   - maintainability
Query suites:
   - csharp-security-and-quality.qls
Click to see the query in the CodeQL repository
This rule finds code that performs string concatenation in a loop using the + operator. If the enclosing loop in question is executed many times, the use of the + operator may be inefficient.
Recommendation¶
It is better to use System.Text.StringBuilder for efficiency.
Example¶
class StringConcatenationInLoop
{
    public static void Main(string[] args)
    {
        String numberList = "";
        for (int i = 0; i <= 100; i++)
        {
            numberList += i + " ";
        }
        Console.WriteLine(numberList);
    }
}
Fix With StringBuilder¶
This code performs the same function as the example except it uses StringBuilder so it is more efficient.
class StringConcatenationInLoopFix
{
    public static void Main(string[] args)
    {
        StringBuilder numberList = new StringBuilder();
        for (int i = 0; i <= 100; i++)
        {
            numberList.Append(i);
            numberList.Append(" ");
        }
        Console.WriteLine(numberList);
    }
}
References¶
MSDN: StringBuilder.

