Skip to content

Add string.Create documentation to "Create new strings in .NET"#52316

Open
Copilot wants to merge 5 commits intomainfrom
copilot/add-string-create-documentation
Open

Add string.Create documentation to "Create new strings in .NET"#52316
Copilot wants to merge 5 commits intomainfrom
copilot/add-string-create-documentation

Conversation

Copy link
Contributor

Copilot AI commented Mar 16, 2026

The "Create new strings in .NET" article had no coverage of string.Create() or the string(ReadOnlySpan<char>) constructor overload.

Article updates (creating-new.md)

  • Mentions string(ReadOnlySpan<char>) in the constructor section
  • Adds string.Create to the methods reference table
  • New Create section covering:
    • How string.Create works (single allocation; runtime passes backing buffer directly to callback as Span<char>)
    • Trade-off comparison between string.Create and stackalloc + new string(span): stack vs. heap pressure, size limits, and boxing behavior of the state parameter
    • Guidance on when to prefer each

New snippets

Both C# and VB projects added under docs/standard/base-types/snippets/creating-new/.

// UsingStringCreate
string result = string.Create(5, 'a', (span, firstChar) =>
{
    for (int i = 0; i < span.Length; i++)
        span[i] = (char)(firstChar + i);
});
// result == "abcde"

// UsingSpanConstructor (C# only — stackalloc unavailable in VB)
static string CreateStringFromSpan()
{
    Span<char> span = stackalloc char[5];
    for (int i = 0; i < 5; i++)
        span[i] = (char)('a' + i);
    return new string(span);
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Add string.Create to documentation</issue_title>
<issue_description>On the page for "Creating new strings in .NET", there is no reference to the recently added string.Create() method


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)

@Youssef1313 The heading "Creating Strings Using a Class Constructor" needs to be updated as well. It doesn't mention the constructor that takes a Span. The article doesn't only need to explain these, but should also provide information on which one can be suitable for what situations, i.e. the trade-offs between different approaches.

For example:

using System;

public class MyState
{
    public char GetAlphabetCharacter(int index) => (char)('a' + index);
}

public class Program
{
    public static void Main()
    {
        FirstApproach();
        SecondApproach();
    }

    public static void FirstApproach()
    {
        var myState = new MyState();
        Span<char> span = stackalloc char[5];
        for (int i = 0; i < 5; i++)
        {
            span[i] = myState.GetAlphabetCharacter(i);
        }
        Console.WriteLine(new string(span));
    }
    
    public static void SecondApproach()
    {
        var myState = new MyState();
        string s = string.Create(5, myState, (span, state) =>
        {
            for (int i = 0; i < 5; i++)
            {
                span[i] = myState.GetAlphabetCharacter(i);
            }
        });
        Console.WriteLine(s);
    }
}

Both FirstApproach and SecondApproach method are functionally equivalent. But I believe they work differently underneath the hood and one can be preferred over the other in different situations.

Possibly:

  • FirstApproach depends on stackalloc, which has a limitation on the specified size; otherwise a stackoverflow can happen.
  • SecondApproach seems to depend on heap allocation, so it's not limited in the specified size.

Does that mean we should always use FirstApproach when the size is known to be small? If SecondApproach depends on heap allocation, what's the difference between this approach and allocating an array?

The article needs to be enriched with this information to be more useful.

Note that the article still needs to be beginner friendly, but also should provide some in-depth information on how things works underneath the hood.</comment_new>
<comment_new>@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 article from GitHub.

Thanks again!</comment_new>
<comment_new>@adegeo
@tdykstra since you assigned yourself the other one, I'll assign you this one and close the other two duplicates.</comment_new>


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.


Internal previews

📄 File 🔗 Preview link
docs/standard/base-types/creating-new.md Create new strings in .NET

…ppets

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot AI changed the title [WIP] [c76a8206] Add string.Create method to documentation Add string.Create documentation to "Create new strings in .NET" Mar 16, 2026
Copilot AI requested a review from BillWagner March 16, 2026 14:13
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 is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review March 16, 2026 18:20
@BillWagner BillWagner requested review from a team and adegeo as code owners March 16, 2026 18:20
Copilot AI review requested due to automatic review settings March 16, 2026 18:20
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 Create new strings in .NET documentation to cover modern high-performance string creation APIs (string.Create and the string(ReadOnlySpan<char>) constructor), and adds runnable C# and Visual Basic snippets to accompany the new content.

Changes:

  • Adds a string(ReadOnlySpan<char>) mention in the constructor section, and adds string.Create to the methods table.
  • Introduces a new Create section describing how string.Create works and when to use it vs. stackalloc + new string(span).
  • Adds new C# and VB snippet projects under docs/standard/base-types/snippets/creating-new/.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
docs/standard/base-types/creating-new.md Adds string.Create coverage, constructor overload mention, and new guidance section with embedded snippets.
docs/standard/base-types/snippets/creating-new/csharp/creating-new.csproj New C# snippet project to build and run sample code used by the article.
docs/standard/base-types/snippets/creating-new/csharp/Program.cs Adds UsingSpanConstructor and UsingStringCreate C# snippet regions.
docs/standard/base-types/snippets/creating-new/vb/creating-new.vbproj New VB snippet project for the String.Create example.
docs/standard/base-types/snippets/creating-new/vb/Program.vb Adds UsingStringCreate VB snippet region.

BillWagner and others added 2 commits March 16, 2026 14:28
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: Bill Wagner <wiwagn@microsoft.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.

Add string.Create to documentation

4 participants