Skip to content

C#: Update StringBuilder query help links to HTTPS - #23268

Merged
michaelnebel merged 1 commit into
github:mainfrom
cjyhjy:csharp/performance-https-stringbuilder
Nov 27, 2026
Merged

michaelnebel merged 1 commit into
github:mainfrom
cjyhjy:csharp/performance-https-stringbuilder

Conversation

@cjyhjy

@cjyhjy cjyhjy commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Update two references in C# Performance query help to the Microsoft Learn HTTPS page for System.Text.StringBuilder. Both links keep their existing text.

Related to #5163.

Validation: git diff --check passed. Both files parsed as XML and rendered to Markdown with codeql generate query-help (CLI 2.27.6, warnings treated as errors). Both rendered links point to the expected Microsoft Learn page.

AI assistance: OpenAI Codex prepared the URL changes and ran the validation above.

@cjyhjy
cjyhjy requested a review from a team as a code owner November 26, 2026 19:33
Copilot AI adaptive review requested due to automatic review settings November 26, 2026 19:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

Both affected query-help references now use the corresponding HTTPS Microsoft Learn URL while preserving their existing link text, with no active issues identified.

No open issues 🎉

What changed in this PR

Updates the C# Performance query-help references for System.Text.StringBuilder from legacy HTTP MSDN URLs to the corresponding HTTPS Microsoft Learn page, preserving the existing link text.

Changes:

  • Updated the StringBuilder reference in StringConcatenationInLoop.qhelp.
  • Updated the StringBuilder reference in StringBuilderInLoop.qhelp.
File Description
csharp/​ql/​src/​Performance/​StringConcatenationInLoop.qhelp Replaces the HTTP MSDN link with the HTTPS Microsoft Learn URL.
csharp/​ql/​src/​Performance/​StringBuilderInLoop.qhelp Replaces the HTTP MSDN link with the HTTPS Microsoft Learn URL.

Review effort: Lite (adaptive)

@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

csharp/ql/src/Performance/StringBuilderInLoop.qhelp

StringBuilder creation in loop

Creating a StringBuilder in the body of a loop is inefficient. It is more efficient to create the StringBuilder before the loop and reuse the same instance for each iteration. Use the Clear method to reset the StringBuilder, which reuses its internal buffer and is more efficient. This is particularly important in performance-critical code.

Recommendation

Create the StringBuilder before the loop, and call the Clear method within the loop to clear the internal buffer.

Example

The following example creates a new StringBuilder in the body of the loop.

static void Main(string[] args)
{
    foreach (var arg in args)
    {
        var sb = new StringBuilder();  // BAD: Creation in loop
        sb.Append("Hello ").Append(arg);
        Console.WriteLine(sb);
    }
}

The code has been rewritten so that the same StringBuilder is used for every iteration of the loop.

static void Main(string[] args)
{
    var sb = new StringBuilder();  // GOOD: Creation outside loop
    foreach (var arg in args)
    {
        sb.Clear();
        sb.Append("Hello ").Append(arg);
        Console.WriteLine(sb);
    }
}

References

csharp/ql/src/Performance/StringConcatenationInLoop.qhelp

String concatenation in loop

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

@michaelnebel michaelnebel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@michaelnebel
michaelnebel merged commit 6a117f8 into github:main Nov 27, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants