skip to content

Why does a C# switch statement reject one case section running into the next, and how do stacked labels and goto case replace fall-through?

level: middleimportance: must knowfreq 60%

answer

  1. end of a section must be unreachable
  2. CS0163 and its final-section twin
  3. several labels on one section
  4. goto case needs a constant label

basics

~20 s

C# requires the end of every switch section to be unreachable, so implicit fall-through is a compile error (CS0163). Several case labels may share one section, and goto case or goto default jumps to another section explicitly.

solid answer

~40 s

In a C# `switch` statement the end point of each section's statement list must be unreachable: the section has to finish with `break`, `return`, `throw`, `goto case`, `goto default`, a `continue` that targets an enclosing loop, or a loop that never ends. If control could run off the end, the compiler reports CS0163, "Control cannot fall through from one case label ('case "raise":') to another", or CS8070 for the last section. The rule exists because silent fall-through is a classic bug. Code that genuinely shares behaviour stacks labels on one section (`case "raise": case "lift":`), and a section that must continue into another says so with `goto case "status";`, which needs a constant label that exists in the same switch (CS0159 otherwise).

code

csharp · 24 lines
csharp
Handle("raise");
Handle("lift");
Handle("lower");
Handle("reboot");

static void Handle(string command)
{
    switch (command)
    {
        case "raise":
        case "lift":                 // stacked labels share one section
            Console.WriteLine("barrier up");
            goto case "status";      // explicit jump, never implicit fall-through
        case "lower":
            Console.WriteLine("barrier down");
            goto case "status";
        case "status":
            Console.WriteLine("  state reported");
            break;
        default:
            Console.WriteLine($"unknown command '{command}'");
            break;
    }
}

go deeper

for a junior

Remember that every C# switch section needs an exit such as break or return, and that several case labels may sit on one section to share code.

for a middle

Explain the reachability rule behind CS0163 and CS8070, list the statements that satisfy it, and show goto case and goto default as the explicit form of fall-through.

for a senior

In review, spot a section whose exit sits in only one branch of an if, and prefer a shared method over long goto case chains between sections.

for a principal

There is no team-level decision here beyond style; the rule is enforced by the compiler, so standards only need to say when goto case chains should become methods.

A **switch statement** picks one **switch section** (a list of `case` labels followed by statements) and runs its statements. In some older languages, control that reaches the end of one section silently continues into the next. C# refuses to compile that, and the reasons and workarounds come up whenever someone reviews a command dispatcher. ## The rule: the end of a section must be unreachable The compiler runs reachability analysis on each section. If the point just after the last statement can be reached, the section would fall through, and that is an error: - **CS0163** for a section followed by another: "Control cannot fall through from one case label ('case "raise":') to another". - **CS8070** for the last section: "Control cannot fall out of switch from final case label ('default:')". The final section is held to the same rule even though there is nothing after it. The documentation summarises this as "every switch section must end with a `break`, `goto`, or `return`", but the compiler's rule is reachability, so more statements qualify. The SDK accepts each of these as the end of a section: 1. `break;` which leaves the switch. 2. `return;` or `return value;` which leaves the method. 3. `throw` which leaves through an exception. 4. `goto case constant;` or `goto default;` which jumps to another section. 5. `continue;` when the switch sits inside a loop, which starts the loop's next iteration. 6. A loop with no exit, such as `while (true) { }`, because its end point is unreachable. ## Sharing code: stacked labels When two commands do the same thing, put several labels on **one section**: ```csharp case "raise": case "lift": Console.WriteLine("barrier up"); break; ``` This is not fall-through: `case "raise":` has no statements of its own, so both labels label the same statement list. The moment you put a statement between the two labels you have two sections, and the first needs its own exit. ## Continuing into another section: goto case and goto default When one command should do its own work and then run another section, say so: - `goto case "status";` transfers control to the section labelled `case "status":`. The expression must be a **constant** convertible to the switch's governing type (a variable there is CS0150, "A constant value is expected"), and a section with exactly that constant label must exist in the same switch, or the compiler reports CS0159, "No such label 'case 7:' within the scope of the goto statement". - `goto default;` transfers control to the `default` section. - `default` may appear anywhere in the switch; it is chosen only when no other label matches, or when a `goto default;` reaches it. Because the jump is written out, the reader sees the extra behaviour, and moving sections around cannot silently change which code runs after which. | want | C# spelling | compiles? | |---|---|---| | two labels, same code | stacked `case` labels on one section | yes | | run section A then section B | `goto case B;` at the end of A | yes | | run off the end of A into B | nothing at the end of A | no, CS0163 | | run off the end of the last section | nothing at the end | no, CS8070 | ## Why the language chose this - **Accidental fall-through is a common defect**: a forgotten `break` makes one command also perform the next one's work. C# turns that into a compile-time error instead of a production incident. - **Intent is visible**: a reviewer can tell shared handling (stacked labels) from chained handling (`goto case`) at a glance. - **Section order stops mattering for control flow** when every label is a distinct constant, because no section's behaviour depends on what follows it. (With patterns and `when` guards, the order of labels still decides which section matches first; that belongs with pattern matching.) ## Common review findings - A section that ends in an `if` with a `break` in only one branch still reaches its end on the other branch, so it is CS0163. - `goto case` to a label that is a pattern rather than a constant is not available; restructure, or call a shared method from both sections. - Heavy `goto case` chains are legal but read like a state machine; extracting the shared work into a method is usually clearer. Switch **expressions** (`value switch { ... => ... }`) have arms instead of sections and no fall-through question at all; they belong with pattern matching, as do case guards and relational or property patterns in case labels.

  • In a C# switch inside a for loop, can a section end with continue instead of break?
    Yes. `continue` makes the end of the section unreachable, so there is no CS0163; it skips the rest of the loop body and starts the loop's next iteration. It is not a way to leave only the switch: `continue` always targets the nearest enclosing loop, and outside any loop it is CS0139, "No enclosing loop out of which to break or continue".
  • In C#, does goto case work when the target section's label is a relational pattern such as case > 100?
    No. `goto case` takes a constant expression and needs a section labelled with that same constant; a section labelled only by a pattern cannot be its target, and a missing constant label is CS0159. When two pattern-labelled sections must share work, put the work in a method and call it from both.

  • Says C# switch sections fall through to the next one when break is missing.
  • Thinks stacked case labels on one section are a form of fall-through the compiler tolerates.
  • Believes the final switch section may omit its break because nothing follows it.
  • Thinks only break, goto and return can end a switch section, never throw or continue.
  • Believes goto case can target any label, including one written as a pattern.