In C#, a switch statement allows you to control the flow of execution in your code by testing for a specific value and executing a corresponding block of code.
In this programming tutorial, we will take a deep dive into understanding how switch statements work in C# and why they are useful. We will explore the syntax, usage, and common gotchas when working with switch statements.
Read: Top Task Management Software for Developers
What is a C# Switch Statement?
A C# switch statement is a type of selection control statement that allows you to choose one of a number of options based on the value of an expression. Expressions are evaluated once and the results are compared with the case labels. When a match is determined, the code associated with that case label is invoked. If no matches are detected, the default label is used if one is available.
That said, there are certain downsides to using switch statements in C#. First, they can be hard to debug because it’s not always clear which case is being executed. Second, they do not work well with objects or strings that have multiple possible values. Finally, they can lead to code that is hard to maintain if the conditions change often.
C# Switch Statement Use Cases
C# switch statements are a great way to handle multiple conditions in your code. They can be used to handle different input types, different results from method calls, or different values in variables. It can be more efficient to use a switch statement instead of an if/else statement when you need to check a lot of conditions.
For example, if you have a bunch of integers that need to be checked for different ranges, a switch statement can check all of them at once. So, you can use switch statements in lieu of if…else statements to improve readability. If you have a complex set of conditions, breaking it up into multiple switch statements can make it easier to understand.
Read: Introduction to For Loops in C#
How to Program Switch Statements in C#
A C# switch statement allows you to choose from a number of possible execution paths based on the value of a specified expression. The syntax for a C# switch statement is as follows:
switch (expression) { case label1: // Code to execute if expression is equal to label1 break; case label2: // Code to execute if expression is equal to label2 break; default: // Code to execute if none of the above case labels match the value of expression }
The following code listing shows how you can work with the switch statement in C#:
internal class Program { static void Main(string[] args) { int i = 1; switch (i) { case 1: Console.WriteLine("The value of i is 1"); // Executes if the value of i is equal to 1 break; case 2: Console.WriteLine("The value of i is 2"); // Executes if the value of i is equal to 2 break; default: Console.WriteLine("The value of i is not 1 or 2"); // Executes if no match is found break; // This is required only if you include a default case } Console.Read(); } }
Multiple Case Statements in C#
Programmers can also have multiple case statements that have the same code block. Here is some example code that illustrates this:
internal class Program { static void Main(string[] args) { int i = 2; switch (i) { case 0: Console.WriteLine("The value of i is 0"); break; case 1: case 2: Console.WriteLine("The value of i is either 1 or 2"); break; } Console.Read(); } }
Read: Best Online Courses to Learn C#
How to Use enum and Switch Statements in C#
An enum in C# is a value type that comprises a collection of constants. Here is another example that illustrates how you can use enum and switch statements in C#. Consider the following enum:
enum WeekDays { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
We will use the value of the enum, i.e., any of the constants mentioned inside the enum, and pass it as a parameter to a switch statement as shown in the code example below:
int day = (int)WeekDays.Wednesday; switch (day) { case 0: Console.WriteLine("Sunday"); break; 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; }
Note how the enum value has been converted into an integer in the preceding code. The following is the complete code listing for your reference:
enum WeekDays { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; internal class Program { static void Main(string[] args) { int day = (int)WeekDays.Wednesday; switch (day) { case 0: Console.WriteLine("Sunday"); break; 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; } } }
When you execute the preceding code listing, the text “Wednesday” will be displayed at the console window.
Final Thoughts on C# Switch Statements
In a switch statement, developers have the option of including one or more case statements, as well as a default statement (optional). It should be noted that case patterns are evaluated from top to bottom. When the first case pattern matches the expression, the statement in the first case pattern is executed. A switch statement evaluates case patterns vertically, from top to bottom. The compiler will throw an error if an unreachable case is present in a switch statement.
Read more C# programming tutorials and software development tips.