Edit

Use labeled jump statement (IDE0410)

Property Value
Rule ID IDE0410
Title Use labeled jump statement
Category Style
Subcategory Language rules (code-block preferences)
Applicable languages C# 15+
Options csharp_style_prefer_labeled_jump_statements

Overview

For clearer, more concise nested loop control flow, this rule flags code that uses goto statements or Boolean flag patterns to break out of or continue nested loops, when a labeled break or continue statement (C# 15+) could be used instead.

The rule detects the following patterns:

  • A goto statement that jumps to a label immediately after a nested loop, which can be replaced with break <label>.
  • A goto statement that jumps to an empty label at the end of the loop body (effectively continuing the outer loop), which can be replaced with continue <label>.
  • A Boolean flag pattern, where a flag variable is set to true and then checked at each loop level to propagate a break or continue outward through nested loops, which can be replaced with a single labeled break <label> or continue <label>.

Options

Options specify the behavior that you want the rule to enforce. For information about configuring options, see Option format.

csharp_style_prefer_labeled_jump_statements

Property Value Description
Option name csharp_style_prefer_labeled_jump_statements
Option values true Prefer labeled jump statements
false Disables the rule
Default option value true

Example

The following examples show the three patterns this rule detects:

goto replaced by break label

// Code with violations.
for (int x = 0; x < 10; x++)
{
    for (int y = 0; y < 10; y++)
    {
        if (x * y > 20)
            goto found;
    }
}

found:
Console.WriteLine("Done");

// Fixed code.
found: for (int x = 0; x < 10; x++)
{
    for (int y = 0; y < 10; y++)
    {
        if (x * y > 20)
            break found;
    }
}

Console.WriteLine("Done");

goto replaced by continue label

// Code with violations.
for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        if (j == 5)
            goto next;
    }

    next: ; // The empty statement is required; a label in C# must be followed by a statement.
}

// Fixed code.
next: for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        if (j == 5)
            continue next;
    }
}

Flag pattern replaced by break label

// Code with violations.
bool found = false;
for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        if (i * j > 20)
        {
            found = true;
            break;
        }
    }

    if (found)
        break;
}

// Fixed code.
loop_i: for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        if (i * j > 20)
        {
            break loop_i;
        }
    }
}

Suppress a warning

If you want to suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable IDE0410
// The code that's violating the rule is on this line.
#pragma warning restore IDE0410

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.cs]
dotnet_diagnostic.IDE0410.severity = none

To disable all of the code-style rules, set the severity for the category Style to none in the configuration file.

[*.cs]
dotnet_analyzer_diagnostic.category-Style.severity = none

For more information, see How to suppress code analysis warnings.

See also