SharpRuneOpen playground

C# 15 labeled break and continue

Name the outer loop instead of carrying a Boolean flag through nested loops.

  • 15 min
  • Read, predict, then run
External .NET 11 SDK / stable C# 15. This project does not run in SharpRune's .NET 9 playground.

Read or download the complete SDK project, then follow its setup on your computer. The separate C# companion practice below runs in SharpRune’s .NET 9 console environment.

At a glance

A stop flag here, a labeled jump in C# 15

SharpRune sample (.NET 9)
  • Inner break leaves only the inner loop
  • stop flag checked after the inner loop
  • Two places encode one exit rule
External project (C# 15)
  • scan: foreach (int[] row in grid)
  • continue scan; skips a damaged row
  • break scan; leaves both loops
  • Unlabeled break and continue are unchanged

Both versions visit three rows and print rows:3 and total:25. A negative cell skips the rest of its row; zero ends the scan before row four.

The concept and environment

C# 15 lets break and continue name a label placed on an enclosing statement. break scan; leaves the loop labeled scan, and continue scan; starts its next iteration, even from inside a nested loop. Without a label, both statements keep their old meaning and target the innermost loop. The external .NET 11 project uses the labeled forms. SharpRune's .NET 9 companion uses the older workaround, a Boolean flag checked after the inner loop. It compares control flow and results; it does not emulate the new statements.

Walk through the portable program

The grid has four rows. A negative cell marks a damaged row whose remaining cells are skipped, and zero marks the end of the data. Row one adds one, two and three for a total of six. Row two adds four, then meets minus one and moves on, skipping the six. Row three adds seven and eight, reaching twenty-five, then meets zero and stops. Row four is never visited. Output is rows:3 and total:25. The companion relies on an inner break plus the stop flag, checked once after every row.

Compile the genuine external feature

In the external project, the outer foreach carries the label scan:. Inside the inner loop, continue scan; abandons the damaged row and starts the next one, and break scan; leaves both loops at once. No flag and no check after the inner loop are needed. The program prints the same two lines as the companion. The project targets net11.0 and selects stable language version 15.0, so a successful build exercises the genuine labeled statements rather than the older goto statement.

Pitfalls and limits

A label applies only to the statement immediately after it; in a: b: while (...), the loop is labeled b. A labeled continue can target only a loop, never a switch statement, and inside a switch section within a loop an unlabeled break still leaves only the switch. Labeled jumps do not replace extracting a helper method when the nested loops compute a value; returning from a method is often the clearest exit. In the companion, the inner break is correct only because nothing follows the inner loop except the flag check.

Practice one: isolate a contract

First, change the end marker from zero to nine in both versions and predict the result. Hint: with zero no longer special, row three adds it and continues, so row four is visited and its first nine ends the scan; work the row count and total out on paper first. Then add a statement after the inner loop in the companion, such as a counter for completed rows, and notice that the flag version now needs extra care that the labeled continue does not.

Practice two: challenge an assumption

For another exercise, rewrite the scan as a local function that returns the total, using return to leave both loops. Compare the three versions: flag, labeled jump and method. Hint: the method form needs a different way to report the row count, such as a tuple result. Enable the IDE0410 style rule in an external editor configuration and see whether it suggests a labeled jump for the companion's flag pattern; treat that suggestion as a review prompt, not as an automatic rewrite. Keep whichever version states the exit rule most directly.

Review the resulting contract

Review labeled break and continue by checking that each label names the loop a reader would expect, and that the scan's rules stay visible: what skips a row and what ends the data. The labeled form removes bookkeeping that the flag version spreads across two places, which makes a later change less likely to break the exit rule. It is not a general replacement for goto or for well-named methods. The external project proves the new statements compile; the portable companion shows the same grid rules with the older flag.

External .NET 11 SDK / stable C# 15. This project does not run in SharpRune's .NET 9 playground.

C# 15 labeled break and continue lab

Set up the SDK project

  1. Save every listed file at its exact relative path in a fresh directory.
  2. Install a .NET 11 SDK supporting stable C# 15; check dotnet --version.
  3. Run dotnet run --project FeatureLab.csproj. Keep this external code separate from the .NET 9 playground.

Download complete SDK project ↗

Project files

Program.csDownload file ↗
Program.cs
int[][] grid = [[1, 2, 3], [4, -1, 6], [7, 8, 0], [9, 9, 9]];
int rows = 0, total = 0;
scan: foreach (int[] row in grid)
{
    rows++;
    foreach (int cell in row)
    {
        if (cell < 0) continue scan;
        if (cell == 0) break scan;
        total = checked(total + cell);
    }
}
Console.WriteLine($"rows:{rows}");
Console.WriteLine($"total:{total}");
FeatureLab.csprojDownload file ↗
FeatureLab.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <LangVersion>15.0</LangVersion>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

Expected result

Expected console output, in this order: rows:3 total:25 The portable companion compares the same small rule using older syntax; it does not compile or emulate the new feature.

Use the listed SDK and dependencies for this project. Open C# practice runs the separate portable exercise.

C# companion practice

Program.cs
using System;
int[][] grid = [[1, 2, 3], [4, -1, 6], [7, 8, 0], [9, 9, 9]];
int rows = 0, total = 0;
bool stop = false;
foreach (int[] row in grid)
{
    rows++;
    foreach (int cell in row)
    {
        if (cell < 0) break;
        if (cell == 0) { stop = true; break; }
        total = checked(total + cell);
    }
    if (stop) break;
}
Console.WriteLine($"rows:{rows}");
Console.WriteLine($"total:{total}");
Open C# practice

C# practice output

rows:3
total:25
Try this
Trace the grid by hand first: a negative cell skips the rest of its row, zero ends the scan. Then compare the flag with break scan.

Further reading