switch expression
A switch construct that produces a value, with one arm per case and no fall-through between them.
In more detail
Because it evaluates to a value, each arm must produce one, and compilers can check that every possible case is covered. It can be returned or assigned directly.
Examples
qty switch { 0 => "none", 1 => "one", _ => "many" }C#: value switch { pattern => result }; a missing case raises warning CS8509.Java: return switch (light) { case RED -> "stop"; case GREEN -> "go"; };return switch (light) { case RED -> "stop"; case GREEN -> "go"; };Related terms
- switch statement — A statement that compares one value against a list of cases and runs the matching branch, with a default for everything else.
- pattern matching — Testing a value against a shape, such as a type, a range or the values of its properties, and optionally extracting parts of it in the same step.
- discard — The underscore _, used in a pattern to match anything, such as the final catch-all arm of a switch expression.
- exhaustiveness — The property of a switch expression whose arms cover every possible input value, which the compiler checks for you.
- CS8509 — The C# compiler warning that a switch expression does not handle all possible input values, naming one that no arm matches.
- exhaustive switch — A switch that the compiler has proven handles every possible value, so it needs no default branch.
- enum — A type that allows only a fixed list of named values, such as admin or user.
- sealed type — A class or interface that lists exactly which types may extend or implement it, so the compiler knows the complete family.
Taught in
- The C# Language: Types, Variables, and Flow Control — Modern .NET Core, C# & Enterprise Microservices
- Records, Pattern Matching, and Modern C# — Modern .NET Core, C# & Enterprise Microservices
- Sealed Types And Exhaustiveness — Modern Java: Virtual Threads & High-Throughput Services
- Pattern Matching And Text Blocks — Modern Java: Virtual Threads & High-Throughput Services
- Dates, Times And Daylight Saving — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Switch, Loops And Labels — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Values And Flow — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Inheritance, Overriding And Object's Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sealed Types, Casts And Pattern Matching — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Mock Exam: Sit It, Score It — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Final Review And Exam Day — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
References
- switch expression
- Java 21 language guide: Switch Expressions and Statements
- JEP 361: Switch Expressions
- Java Language Specification, chapter 15: Expressions