C# Switch Statements
In C#, the switch
statement is used to execute one of several blocks of code depending on the value of an expression. It's similar to the if-else
statement but can be more concise and easier to read, especially when testing for multiple values.
Here's the basic syntax of a switch
statement:
1switch (expression) 2{ 3 case value1: 4 // code to execute if expression == value1 5 break; 6 case value2: 7 // code to execute if expression == value2 8 break; 9 default: 10 // code to execute if expression doesn't match any case 11 break; 12}
The expression
is evaluated once and compared to each case
value until a match is found. If there's no match, the default
block of code is executed. Each case
block of code must end with a break
statement, which terminates the switch
statement and exits the block.
Here's an example of a switch
statement that tests the value of a variable dayOfWeek
:
1string dayOfWeek = "Monday"; 2switch (dayOfWeek) 3{ 4 case "Monday": 5 Console.WriteLine("Today is Monday"); 6 break; 7 case "Tuesday": 8 Console.WriteLine("Today is Tuesday"); 9 break; 10 case "Wednesday": 11 Console.WriteLine("Today is Wednesday"); 12 break; 13 case "Thursday": 14 Console.WriteLine("Today is Thursday"); 15 break; 16 case "Friday": 17 Console.WriteLine("Today is Friday"); 18 break; 19 case "Saturday": 20 Console.WriteLine("Today is Saturday"); 21 break; 22 case "Sunday": 23 Console.WriteLine("Today is Sunday"); 24 break; 25 default: 26 Console.WriteLine("Invalid day of the week"); 27 break; 28}
This code will output the following text, because the value of dayOfWeek
is "Monday":
Today is Monday
Note that the case
values are strings, but they could be any data type that can be compared, such as integers or enums.
Multiple Cases
Sometimes you may want to execute the same block of code for multiple values. In this case, you can combine case
statements as follows:
1int num = 3; 2switch (num) 3{ 4 case 1: 5 case 2: 6 case 3: 7 Console.WriteLine("The number is 1, 2, or 3"); 8 break; 9 case 4: 10 Console.WriteLine("The number is 4"); 11 break; 12 default: 13 Console.WriteLine("The number is not 1-4"); 14 break; 15}
In this example, if the value of num
is 1, 2, or 3, the first case
block of code will be executed. If the value of num
is 4, the second case
block of code will be executed. Otherwise, the default
block of code will be executed.
Fall-through
In C# switch statements, it's possible to have "fall-through" behavior, which means that when a case matches, the code will execute that case and all the cases below it until a "break" statement is encountered.
Here's an example:
1int num = 2; 2switch (num) 3{ 4 case 1: 5 Console.WriteLine("One"); 6 break; 7 case 2: 8 Console.WriteLine("Two"); 9 goto case 1; // this will cause fall-through behavior 10 case 3: 11 Console.WriteLine("Three"); 12 break; 13}
In this example, the variable num
is set to 2. When the switch statement is executed, it will match the case 2
and execute the code within it, which is to print "Two" to the console. However, it also includes a goto
statement to the case 1
, which causes fall-through behavior. This means that the code in case 1
will also be executed, and "One" will be printed to the console.
It's worth noting that using fall-through behavior in switch statements can make the code harder to read and understand, especially if there are many cases involved. It's generally recommended to use break statements to explicitly define the end of a case, rather than relying on fall-through behavior.
Switch Default
The switch statement can also include a default case which is executed when none of the other cases match the expression. The default case does not need to appear at the end of the switch statement, but it is conventionally placed there.
Here is an example of a switch statement with a default case:
1int dayOfWeek = 3; 2string dayName; 3 4switch (dayOfWeek) 5{ 6 case 1: 7 dayName = "Monday"; 8 break; 9 case 2: 10 dayName = "Tuesday"; 11 break; 12 case 3: 13 dayName = "Wednesday"; 14 break; 15 case 4: 16 dayName = "Thursday"; 17 break; 18 case 5: 19 dayName = "Friday"; 20 break; 21 default: 22 dayName = "Unknown"; 23 break; 24} 25 26Console.WriteLine($"The day of the week is {dayName}");
In this example, the dayOfWeek
variable is set to 3, and the switch statement assigns the corresponding day name "Wednesday" to the dayName
variable. If the value of dayOfWeek
was not one of the cases specified, the default case would be executed and the dayName
variable would be set to "Unknown".
It's important to note that each case block must end with a break
statement, which prevents the code from "falling through" to the next case block. If a break statement is omitted, the next case block will be executed regardless of whether the expression matches it.
In addition to using constant expressions in switch statements, C# also allows the use of pattern matching in switch statements. Pattern matching allows you to match on more complex expressions and perform more complex logic in each case block.
Here is an example of a switch statement using pattern matching:
1object obj = "hello"; 2string result = ""; 3 4switch (obj) 5{ 6 case string str: 7 result = $"The string is '{str}'"; 8 break; 9 case int i when i > 0: 10 result = $"The number is positive: {i}"; 11 break; 12 case int i: 13 result = $"The number is negative: {i}"; 14 break; 15 default: 16 result = "Unknown type"; 17 break; 18} 19 20Console.WriteLine(result);
In this example, the obj
variable is assigned the value "hello", and the switch statement matches on the type of obj
. Since obj
is a string, the first case block is executed and the result is "The string is 'hello'".
The second case block uses a when clause to test whether the integer value is greater than zero. If it is, the corresponding message is printed. If not, the third case block is executed, and the integer value is assumed to be negative.
The default case block is executed if none of the other cases match.