Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The goto statement

Reference for C# goto: jumping to labels, goto case and goto default in switch, leaving nested loops, scope restrictions and why it is rarely needed.

goto transfers control unconditionally. It has three forms: goto label; jumps to a labelled statement in the same method, while goto case value; and goto default; jump to another section of the enclosing switch. A label is an identifier followed by a colon placed before a statement.

Structured statements (loops, switch, early return) cover almost every need, which is why goto is rare in C#. It still has two legitimate uses: breaking out of several nested loops at once and explicitly continuing into another switch section.

C#
for (int r = 0; r < rows; r++)
    for (int c = 0; c < cols; c++)
        if (grid[r, c] == target) goto found;

Console.WriteLine("missing");
return;
found:
Console.WriteLine("present");

Try it

C# Leaving nested loops

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# goto case and goto default

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
label:
    statement

goto label;
goto case constant-expression;
goto default;
Name Description
label An identifier declared in the same method (or lambda). Its scope is the whole block where it is declared, including nested blocks.
constant-expression A case label of the enclosing switch (must match one of its constants).

Errors: a label that is never used gives warning CS0164; jumping to a label that does not exist or is out of scope gives CS0159. You cannot jump into a block (CS0159 again), out of a finally block (CS0157), or into a try. A label must be followed by a statement; use label:; or label: { } when there is nothing to execute.

Usage notes

  • Leaving blocks only: goto may jump out of nested blocks (including loops and try blocks, which runs pending finally code) but never into one.
  • Backward jumps to an earlier label in the same or an enclosing block build loops by hand. They compile, but a real loop is clearer.
  • Alternatives for nested loops: extract the search into a method and return, or use LINQ (Any, FirstOrDefault). These need no label and make the code testable.
  • goto case is C#'s only way to continue into another switch section, because sections do not fall through. Often a shared helper method is cleaner.
  • Readability: keep any goto close to its label, and use a descriptive label name such as done or retry.
  • Retry pattern: retry: at the top of a block with goto retry; in a catch works, but a loop with a counter is easier to bound.
  • No performance benefit: the compiler produces the same branches for loops and goto.