Document labeled break and continue (C# 15)#54999
Conversation
There was a problem hiding this comment.
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
breakandcontinue” section to the C# 15 “What’s new” page. - Extends the jump statements reference to explain labeled targets for
breakandcontinue, and adds new snippet examples. - Publishes the speclet by wiring it into the C# specification TOC and
docfx.jsonproposal 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. |
|
@BillWagner, I've reviewed the PR and found the cause of the build failures: Issue 1: CS8652 errors in snippets** Fix: In <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! |
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
cc6ee88 to
54f1bbf
Compare
Thanks. I did know it was coming in preview, but hadn't updated the project files yet. |
gewarren
left a comment
There was a problem hiding this comment.
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`. |
There was a problem hiding this comment.
When would you use two labels like that?
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.
…r/docs into labeled-break-continue
Summary
Documents the C# 15 (.NET 11) labeled
breakandcontinuefeature. Fixes #54653.Scope is limited to the C# reference, publishing the speclet, and the What's new in C# 15 article.
Changes
jump-statements.md): extended thebreakandcontinuesections to cover labeled targets, clarified thatcontinuetargets loops only (notswitch), and added the "only the immediately-nested statement is labeled" nuance. New snippet examplesLabeledBreakandLabeledContinuein the jump-statements snippet project.csharp-15.md): new bullet + "Labeledbreakandcontinue" section.specification/toc.yml(Feature specifications → Statements) and todocfx.json(build include, title, and description metadata), following the pattern from Addclosedhierarchies for C# 15, preview 5 #54128.Notes for reviewers
.csfiles 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.labeled-break-continue.md.Internal previews
break,continue,return, andgoto