As we will see, conditional statement switch works very much like the If-Else If-Else statement, with only a syntax difference. It executes an instruction based on the calculated value of a condition. The format of the switch statement is the following:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
switch (condition)
{
case value1:
instructions;
break;
case value2:
instructions;
break;
case value3:
instructions;
break;
// ...
default:
instructions;
break;
}
}
}
}
|
The above code could have been written as:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
if (condition == value1)
instructions;
else if (condition == value2)
instructions;
else if (condition == value3)
instructions;
// ...
else
instructions;
}
}
}
|
We have a condition, for which we check if its result is equal to value1, and execute some instructions if it’s true; if not, we check that result against the next value, value2, executing some other instructions if it matches, and so on. Finally, if none of the conditions are true, we have a default statement, which is similar to the else statement, and which executes only if none of the values matched the value of the condition.
Aside of that, you’ve noticed the keyword break. As you already know, a switch statement performs a conditional processing, and you can define one or more possible cases against which it can check the value of the condition. For each case, we specify the instructions we want to be executed when that case is true, and a break instruction at the end of the instructions, to separate a case instruction from another. If you don’t place a break statement, the execution will continue with the instructions that follow, without checking the case with which those instructions are associated. Let’s consider the following example:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
char character = 'A';
switch (character)
{
case 'A': Console.WriteLine("The character is A");
case 'B': Console.WriteLine("The character is B");
case 'C': Console.WriteLine("The character is C");
}
}
}
}
|
First, the program checks the character variable is equal to character A and executes an instruction if it’s true, printing the "The character is A" message. Everything good so far. However, because no break instruction follows, the program will continue the execution and will also display the messages "The character is B" and "The character is C". Because of this, if no code is found after the case statement, the break instruction can be omitted, since the program will simply jump to the next case statement, until it will find a break operator. However, after the default case, a break instruction is mandatory.
You can also use multiple cases for the same instruction, as following:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int number = 8;
switch (number)
{
case 1:
case 3:
case 5:
case 7:
Console.WriteLine("Number is odd");
break;
case 2:
case 4:
case 6:
case 8:
Console.WriteLine("Number is even");
break;
default:
Console.WriteLine("Unknown number");
break;
}
}
}
}
|
There are times when the number of possible cases is limited and we check them all. In such case, it is still advisable to place a default check, in which you can print an error message, for instance.
It is considered a good practice to place the default case at the end of all the other cases.
In C# 8.0, a shorter and more expressive way to handle multiple cases and return values based on conditions was introduced: switch expressions. They provide a concise syntax to assign values based on a condition. Here's how switch expressions work:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
var result = condition switch
{
value1 => instructions,
value2 => instructions,
value3 => instructions,
// ...
_ => default instructions
};
}
}
}
|
The switch expression starts with the condition, followed by the switch keyword. Each value is associated with an expression using the => operator. When the condition matches a value, the corresponding expression is evaluated and assigned to the result variable. If none of the cases match, _ (discard variable) represents the default case, and the default instructions are assigned. By using the underscore, you indicate that you don't need to handle or assign a specific value. It acts as a catch-all for any values that don't match the explicitly defined cases. This can be useful when you only want to handle specific cases and provide a default behavior for all other cases.
Switch expressions offer a more compact and readable alternative to if-else statements for simple conditions. They are especially useful when assigning values based on multiple conditions, but they may not be suitable for complex logic.
The concepts explained in this lesson are also shown visually as part of the following video: