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.
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
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.
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
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:
gotomay jump out of nested blocks (including loops andtryblocks, which runs pendingfinallycode) 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 caseis 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
gotoclose to its label, and use a descriptive label name such asdoneorretry. - Retry pattern:
retry:at the top of a block withgoto retry;in acatchworks, but a loop with a counter is easier to bound. - No performance benefit: the compiler produces the same branches for loops and
goto.