C# switch Statement

The objective of this article and youtube video is to familiarize you with the C# switch statement, also known as switch case in C#. You will learn switch statement with code examples, default label, goto statement, and finally grouped cases.

You will learn

The C# switch statement is a control statement different from the if statement because it evaluates a single expression against a list of multiple possible cases. Every switch case is related to the single expression and must end with the break or goto case or an empty block. The break statement passes control out of the switch. You can omit a break statement if two cases lead to the same action.

C# switch statement may result in cleaner code than multiple if statements since switch statements require an expression to be compared only once.

C# switch Syntax

A switch statement starts by using the switch keyword followed by a switch expression in parentheses. This expression must evaluate the built-in integral types; the bool, char, and enum types; and the string type. After the switch expression, you include multiple case labels that represent the possible values of the switch expression. When the switch expression finds the matching value specified by a case label, the statements inside that case are executed. C# switch statement can also contain a default label that is executed if none of the values specified by the previous case labels match with the switch expression.

When you code a case label or a default label, you must add a colon “:” at the end. Then, if the label contains one or more statements, you must use a break statement to terminate the execution of a switch statement.

C# switch statement flowchart

C# switch Statement with a default Label

You can have as many cases as you want in whatever sequence possible when using a switch statement. The default case is optional and is executed if no other case applies. It’s also a common practice to code the default label last for easy reading.

In the following example, the value of a number variable is 4. None of the cases matches with the value provided by the switch expression. In this case, the default block is executed which prints out “Unknown number!”

Run Demo


Unknown number!

C# goto switch Statement

You can use the goto keyword followed by a case label and a semicolon (;) at the end, which will cause the execution to jump to a new location in the code. The target location is defined by the marker in the code, which is called a label. Check the example below.

Run Demo


Case: B

C# switch Statement That Falls Through the Case Label

If one or more case labels are empty, the execution will follow the code of the next case block that contains code. This allows for grouping of multiple case labels with the same implementation.

In the following example, if the number equals 1, the code in case 2 will be executed because the case label 1 and 2 are grouped. If a case block is not empty, a break must be present before the next case label; otherwise, the compiler will generate an error.

Run Demo


Case: One and Two

Useful Article

C# Reference

User Rating 0 (0 vote)
Tagged in :
Send this to a friend