Skip to content

Document labeled break and continue (C# 15)#54999

Open
BillWagner wants to merge 5 commits into
dotnet:mainfrom
BillWagner:labeled-break-continue
Open

Document labeled break and continue (C# 15)#54999
BillWagner wants to merge 5 commits into
dotnet:mainfrom
BillWagner:labeled-break-continue

Conversation

@BillWagner

@BillWagner BillWagner commented Jul 22, 2026

Copy link
Copy Markdown
Member

Summary

Documents the C# 15 (.NET 11) labeled break and continue feature. Fixes #54653.

Scope is limited to the C# reference, publishing the speclet, and the What's new in C# 15 article.

Changes

  • Language reference (jump-statements.md): extended the break and continue sections to cover labeled targets, clarified that continue targets loops only (not switch), and added the "only the immediately-nested statement is labeled" nuance. New snippet examples LabeledBreak and LabeledContinue in the jump-statements snippet project.
  • What's new in C# 15 (csharp-15.md): new bullet + "Labeled break and continue" section.
  • Speclet publishing: added the speclet to specification/toc.yml (Feature specifications → Statements) and to docfx.json (build include, title, and description metadata), following the pattern from Add closed hierarchies for C# 15, preview 5 #54128.

Notes for reviewers

  • ⚠️ Draft — syntax not yet verified. The feature bits haven't shipped, so nothing compiles yet. The two new snippet .cs files use the speclet syntax and their // Output: blocks are reasoned by hand. These will be verified against early bits before the PR is marked ready.
  • Speclet source: labeled-break-continue.md.

Internal previews

📄 File 🔗 Preview link
docs/core/whats-new/dotnet-11/overview.md What's new in .NET 11
docs/csharp/language-reference/statements/jump-statements.md Jump statements - break, continue, return, and goto
docs/csharp/specification/toc.yml docs/csharp/specification/toc
docs/csharp/whats-new/csharp-15.md What's new in C# 15

Copilot AI review requested due to automatic review settings July 22, 2026 18:43
@dotnetrepoman dotnetrepoman Bot added this to the July 2026 milestone Jul 22, 2026

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.

Pull request overview

Documents the C# 15 labeled break and continue feature across the language reference, the C# 15 “What’s new” article, and the published feature specification (speclet).

Changes:

  • Adds a new “Labeled break and continue” section to the C# 15 “What’s new” page.
  • Extends the jump statements reference to explain labeled targets for break and continue, and adds new snippet examples.
  • Publishes the speclet by wiring it into the C# specification TOC and docfx.json proposal metadata.

Reviewed changes

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

Show a summary per file
File Description
docs/csharp/whats-new/csharp-15.md Adds a new section and TOC entry describing labeled break/continue.
docs/csharp/specification/toc.yml Adds the proposal under Feature specifications → Statements.
docs/csharp/language-reference/statements/snippets/jump-statements/ContinueStatement.cs Adds a labeled continue snippet example.
docs/csharp/language-reference/statements/snippets/jump-statements/BreakStatement.cs Adds a labeled break snippet example.
docs/csharp/language-reference/statements/jump-statements.md Updates break/continue documentation and references the new snippets.
docfx.json Includes the new proposal in build inputs and adds title/description metadata.

Comment thread docs/csharp/whats-new/csharp-15.md
@MatveevichOne

Copy link
Copy Markdown

@BillWagner, I've reviewed the PR and found the cause of the build failures:

Issue 1: CS8652 errors in snippets**
The labeled break/continue feature is a C# 15 preview feature, but the snippet project doesn't have LangVersion set to preview.

Fix: In docs/csharp/language-reference/statements/snippets/jump-statements/jump-statements.csproj, add:

<LangVersion>preview</LangVersion>
Issue 2: Non-self-contained example in csharp-15.md
The example uses grid.Height, grid.Width, grid.IsBlocked, and grid.IsGoal — these members aren't defined anywhere, so readers can't copy and run the code.
Fix: Replace the current example with this self-contained version using a 2D array:
csharp
string[,] grid = new string[,]
{
    { "Clear", "Clear", "Clear" },
    { "Clear", "Blocked", "Clear" },
    { "Clear", "Clear", "Goal" }
};

outer: for (int row = 0; row < grid.GetLength(0); row++)
{
    for (int column = 0; column < grid.GetLength(1); column++)
    {
        if (grid[row, column] == "Blocked")
        {
            Console.WriteLine($"Blocked at ({row}, {column}) — skipping to next day.");
            continue outer;
        }
        if (grid[row, column] == "Goal")
        {
            Console.WriteLine($"Goal found at ({row}, {column})!");
            break outer;
        }
        Console.WriteLine($"Processing ({row}, {column})");
    }
}
Issue 3 (optional): The snippet project targets net8.0. Consider updating to net9.0 or net10.0 since C# 15 corresponds to .NET 11. But adding LangVersion=preview is sufficient to fix the build.
Let me know if you need help applying these changes!

BillWagner and others added 2 commits July 23, 2026 14:27
Add documentation for the C# 15 labeled `break` and `continue` feature:

- Language reference: extend the `break` and `continue` sections in
  jump-statements.md, with new snippet examples (LabeledBreak,
  LabeledContinue).
- What's new in C# 15: add a "Labeled break and continue" section.
- Publish the feature speclet via the specification TOC and docfx.json
  (build include, title, and description metadata).

The feature bits haven't shipped yet, so snippet output is reasoned from
the speclet and will be verified against early bits.

Fixes dotnet#54653

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: a542e9e6-723c-47d8-95e0-f4c371174bd8
@BillWagner
BillWagner force-pushed the labeled-break-continue branch from cc6ee88 to 54f1bbf Compare July 23, 2026 18:41
@BillWagner

Copy link
Copy Markdown
Member Author

@BillWagner, I've reviewed the PR and found the cause of the build failures:

Thanks. I did know it was coming in preview, but hadn't updated the project files yet.

@BillWagner
BillWagner marked this pull request as ready for review July 23, 2026 18:51
@BillWagner
BillWagner requested a review from a team as a code owner July 23, 2026 18:51

@gewarren gewarren left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I seem to remember an earlier PR that included code examples for labeled break/continue. Did you add links to that content? Also, if I remember correctly, that content explained nicely why you would use this, like instead of a bunch of conditional checks in each nested loop.


:::code language="csharp" source="snippets/jump-statements/ContinueStatement.cs" id="LabeledContinue":::

For both labeled `break` and labeled `continue`, only the statement immediately nested in a labeled statement has that label. For example, in `a: b: while (...)`, the `while` statement is labeled `b`, not `a`.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When would you use two labels like that?

Comment thread docs/csharp/whats-new/csharp-15.md Outdated
BillWagner and others added 3 commits July 24, 2026 10:05
Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
Added a link to the IDE 0410 diagnostic article. That should an example of making use of this new feature.

While searching for that, I noticed that the list of features in the "What's new" in .NET 11 hadn't been updated. Make those updates as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C# 15-Fundamentals and reference]: Labeled break and continue

4 participants