Skip to content

Improve StringBuilder interop guidance: warn about allocations, promote ArrayPool#52133

Open
Copilot wants to merge 6 commits intomainfrom
copilot/update-stringbuilder-best-practices
Open

Improve StringBuilder interop guidance: warn about allocations, promote ArrayPool#52133
Copilot wants to merge 6 commits intomainfrom
copilot/update-stringbuilder-best-practices

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

The "Fixed-length string buffers" section presented StringBuilder as a co-equal alternative to char[]/ArrayPool, with only a mild [!NOTE] about performance. This conflicts with the native interop best practices page, which explicitly recommends avoiding StringBuilder parameters due to excessive allocations.

Changes

  • Promoted ArrayPool<char> as the explicitly recommended approach — updated the intro sentence to call it out as such
  • De-emphasized StringBuilder — changed "Another solution is..." to "You might also consider..." to signal it's secondary
  • Upgraded [!NOTE][!CAUTION] with a detailed breakdown of why StringBuilder is costly:
    • Always creates a native buffer copy
    • Generates up to 4 allocations per P/Invoke call (managed buffer → native buffer → managed array copy → ToString())
    • The capacity doesn't include the null terminator, a common source of bugs and wasted allocations
    • Reusing StringBuilder saves only one allocation vs. ArrayPool<char> which reduces it to just ToString()
  • Added link to CA1838 (Avoid StringBuilder parameters for P/Invokes) alongside the existing best-practices link
Original prompt

This section details on the original issue you should resolve

<issue_title>Efficiency issues with StringBuilder</issue_title>
<issue_description>
The discussion of using StringBuilder makes no mention of the excessive allocations as discussed on the native interop best practices page; the guidance there specifically says to "AVOID StringBuilder parameters." Meanwhile, the recommended best practice ("using char[]s from an ArrayPool") is not mentioned on this page.

I imagine this page has simply fallen out of date, but it'd be nice to present a consistent story about best practices and sample code for guidance.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Comments on the Issue (you are @copilot in this section)

@adegeo Thank you for opening this issue. I'll add it to the backlog. If you would like to contribute, you can submit a fix by editing the original article. Click on the **Content Source** link at the bottom of your original comment. To learn how to edit, see the [Editing files in a repository](https://help.github.com/articles/editing-files-in-your-repository) article from GitHub.

Thanks again!</comment_new>


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.


Internal previews

📄 File 🔗 Preview link
docs/framework/interop/default-marshalling-for-strings.md docs/framework/interop/default-marshalling-for-strings

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot AI changed the title [WIP] Update best practices related to StringBuilder usage Improve StringBuilder interop guidance: warn about allocations, promote ArrayPool Mar 10, 2026
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

This LGTM once I make one change.

@BillWagner BillWagner marked this pull request as ready for review March 11, 2026 12:49
@BillWagner BillWagner requested a review from a team as a code owner March 11, 2026 12:49
@BillWagner BillWagner requested review from Copilot and gewarren March 11, 2026 12:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the .NET Framework interop documentation for string marshalling to better emphasize modern performance guidance and disclose AI-assisted edits.

Changes:

  • Updated frontmatter metadata (ms.date) and added ai-usage: ai-assisted.
  • Revised guidance around using ArrayPool<char> for fixed-length buffers.
  • Expanded and strengthened the StringBuilder performance guidance, including links to CA1838 and interop best practices.

BillWagner and others added 2 commits March 12, 2026 10:47
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…uffer to pool

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
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.

Efficiency issues with StringBuilder

3 participants