switch statements

A switch statement

A switch statement chooses one branch from a list of labels. It is not only for numbers and strings: a label may be a type pattern, a constant pattern or a pattern with a guard.

Console.WriteLine("commands:"); foreach (var command in new[] { "start", "stop", "pause", "restart", "up" }) { switch (command) { case "start": Console.WriteLine("starting"); break; case "stop": case "pause": Console.WriteLine("stopping"); break; case string other when other.Length > 4: Console.WriteLine($"unknown, long: {other}"); break; default: Console.WriteLine("unknown"); break; } }

dotnet run

commands:
starting
stopping
stopping
unknown, long: restart
unknown

Five words, five branches: start matched its own label, stop and pause share one body through two stacked labels, restart fell into the pattern with the guard because it is longer than four characters, and up reached default.

The order of the labels is the order of the tests, and the first match wins. A label with a guard is tested like any other, so a case that would match everything belongs last, next to default.

Stacked labels and when

Three rules differ from C and from C++:

A case with a constant that is already handled is a compile error (CS0152), and a switch over a bool needs both values or a default.

goto case

The one general jump a switch keeps is goto case: a section that has done its own work continues in another section. It is the only way to share code between labels that is not a shared body.

int level = 2; Console.WriteLine($"level {level}:"); switch (level) { case 1: Console.WriteLine("low"); goto case 2; case 2: Console.WriteLine("medium"); break; default: Console.WriteLine("other"); break; }

dotnet run

level 2:
medium

Level 2 matched its own label, so only medium printed; with level 1 the output would be both lines, because the first section jumps into the second. The statement is rare in modern code, since the switch expression of the next lesson returns a value instead of jumping, and a method that does the shared work is usually clearer.

Exercises

  1. Remove the break from the first section and read the error. Which code does it have?
  2. Add a case for "up" and check that default no longer catches it.
  3. Switch over an int with cases 1, 2 and 3 and a default. What does the compiler say when two labels hold the same value?
  4. Rewrite the goto case program with an if and compare the two shapes.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What must end every non-empty case section?

    Answer

    break, return, continue, throw or goto

    C# forbids falling into the next section, so a section that does something must leave the switch.

  2. What does this program print? int n = 2; switch (n) { case 1: Console.WriteLine("one"); break; case 2: Console.WriteLine("two"); break; default: Console.WriteLine("other"); break; }

    Answer

    two

    The value 2 matches its own label, and the break ends the switch after that section.

  3. What does this program print? string word = "abcd"; switch (word) { case string s when s.Length > 3: Console.WriteLine("long"); break; default: Console.WriteLine("short"); break; }

    Answer

    long

    The pattern matches any string and the guard asks for a length above three, which abcd has.

  4. How do two labels share one body?

    Answer

    Write them one after the other with no statements between

    An empty section falls through to the next label, so stacked labels reach the same statements.

  5. What does the compiler do with two case labels of the same constant?

    Answer

    It reports CS0152, because the label is already handled

    A duplicate label can never be reached, so it is a compile error rather than a warning.

  6. What does this program print? int n = 1; switch (n) { case 1: Console.WriteLine("low"); goto case 2; case 2: Console.WriteLine("medium"); break; }

    Answer

    low / medium

    The first section prints and jumps into the second, which prints again and leaves the switch.

  7. Which branch runs when no label matches?

    Answer

    default, when the switch has one

    default is the label for every value the others do not match, and a switch without it simply does nothing.

  8. What does this program print? string s = "b"; switch (s) { case "a": case "b": Console.WriteLine("known"); break; default: Console.WriteLine("other"); break; }

    Answer

    known

    The stacked labels share the section, and b matches the second of them.

  9. What does a when guard add to a case label?

    Answer

    A bool condition that must also be true for the label to match

    The label matches the pattern and then the guard, and both must succeed for the section to run.

  10. What happens in a switch over a bool with only a true case and no default?

    Answer

    It compiles, and false does nothing

    A switch statement does not have to cover every value; a switch expression, by contrast, must.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev