C# Switch Statement
The switch
statement in C# allows you to execute different blocks of code based on the value of a variable or expression. It provides a cleaner and more efficient way to compare a single value against multiple possible cases, especially when dealing with numerous conditions.
Key Topics
1. Syntax of Switch Statement
switch (expression)
{
case value1:
// Code to execute when expression equals value1
break;
case value2:
// Code to execute when expression equals value2
break;
default:
// Code to execute when none of the cases match
break;
}
The switch
statement evaluates the expression and compares it to each case label. If a match is found, the corresponding code block is executed.
2. Basic Switch Statement Example
Example: Day of the Week
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Output:
Code Explanation: The variable day
is compared against each case. Since day
is 3
, it matches case 3
, and "Wednesday" is printed.
3. Switch with Multiple Case Labels
You can group multiple case labels to execute the same block of code. This is useful when different cases share the same logic.
Example: Weekday or Weekend
int day = 6;
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("Weekday");
break;
case 6:
case 7:
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Output:
Code Explanation: The cases for days 1 to 5 print "Weekday", while cases 6 and 7 print "Weekend". Since day
is 6
, "Weekend" is printed.
4. Switch with String Cases
The switch
statement can also work with strings, allowing you to compare string values.
Example: Fruit Selection
string fruit = "apple";
switch (fruit)
{
case "apple":
Console.WriteLine("You chose apple.");
break;
case "banana":
Console.WriteLine("You chose banana.");
break;
case "orange":
Console.WriteLine("You chose orange.");
break;
default:
Console.WriteLine("Unknown fruit.");
break;
}
Output:
Code Explanation: The switch
compares the string fruit
with each case. Since fruit
is "apple", it matches the first case, and the message is printed.
5. Switch Expression (C# 8.0+)
In C# 8.0 and later, you can use switch expressions for more concise and expressive code.
Example: Number to Word Conversion
int number = 4;
string result = number switch
{
1 => "One",
2 => "Two",
3 => "Three",
4 => "Four",
5 => "Five",
_ => "Unknown"
};
Console.WriteLine(result); // Outputs: Four
Output:
Code Explanation: The switch expression matches the value of number
to the corresponding string. The underscore (_
) represents the default case.
6. Best Practices for Switch Statements
- Always include a
default
case to handle unexpected values. - Use the
switch
statement when comparing a single variable against multiple constant values. - Ensure that each case ends with a
break
statement to prevent fall-through. - Consider using switch expressions in C# 8.0+ for cleaner code.
Key Takeaways
- The
switch
statement simplifies multiple conditional checks against a single value. - It enhances code readability and can be more efficient than multiple
if-else
statements. - Switch statements can be used with integral types, strings, and enums.
- Switch expressions provide a concise syntax for simple value mappings.
- Proper use of
break
anddefault
cases is essential for correct control flow.