- The switch case is a decision-making statement like the if else statement in C#.
- The switch statement is used as an alternative (and elegant way) to the if..else if a single expression is tested against three or more conditions.
- For example, we want to check the color selected by the user from the four given options and execute a block of code based on the selected value.
- By using a switch statement, this can be done as follows:
using System; namespace csharp_switch { class Program { static void Main(string[] args) { string color_name = "Green"; switch (color_name) { case "Green": { Console.WriteLine("The color is Green!"); break; } case "Yellow": { Console.WriteLine("The color is Yellow!"); break; } case "Red": { Console.WriteLine("The color is Red!"); break; } case "Black": { Console.WriteLine("The color is Black!"); break; } } Console.ReadLine(); } } }
The same can be done by using the if..else if statement as follows:
using System; namespace ifStatement { class Program { static void Main(string[] args) { string color_name = "Green"; //Using if else statement if (color_name == "Green") { Console.WriteLine("The color is Green!"); Console.ReadLine(); } else if (color_name == "Yellow") { Console.WriteLine("The Color is Yellow!"); Console.ReadLine(); } else if (color_name == "Red") { Console.WriteLine("The Color is Red!"); Console.ReadLine(); } else if (color_name == "Black") { Console.WriteLine("The Color is Black!"); Console.ReadLine(); } } } }
I will explain the use of case statement along with what if none of the options is true i.e. using the default case, let us first look at the general structure of the switch C# switch statement:
Structure of C# switch case statement
Following is the general way of using the switch statement in C#:
switch(expression) { case expression : //Place code to be executed here; break; case constant-expression : //Place code to be executed here; break; //The default case is executed if none of the case is True default : //Place code to be executed here; }
Following are a few key points to note about the switch case statement:
key points | Description |
expression | In C# 6, the expression must return a value as bool, char, string, or an integral value like int or long.
It may also return an enum value (see an example in the later part). |
case |
|
value in the case | The value used in the case statement must be the same as used in the switch expression.
For example, we used a string expression in the switch (in the above example). So, the cases also used string values to be tested for. |
break | The break is an optional statement in each case.
As the break statement is executed, the switch execution terminates and the control moves to the next line after the switch block. |
no break statement | If no break statement is used then subsequent cases keep on tested. |
default block |
|
Now, let us look at a few examples of using the switch case statement including using an Enum expression.
An example of switch case with int expression
In the first switch case demo, I used an int expression in the switch statement. In this basic example, we have three cases and a default case.
The value for the variable is set at the time of declaration and that variable name is used as an expression in the switch statement.
In three cases, I checked numeric values and wrote a respective statement to execute.
If none of the cases matches, the default case will execute.
Have a look at the code and output as I set the value of the variable as 15:
using System; namespace csharp_examples { class Program { static void Main(string[] args) { int a_num = 150; switch (a_num) { case 5: { Console.WriteLine("The case for value 5 is executed!"); break; } case 10: { Console.WriteLine("The case for value 10 is executed!"); break; } case 15: { Console.WriteLine("The case for value 15 is executed!"); break; } default: { Console.WriteLine("Some other value than 5, 10, and 15."); break; } } Console.ReadLine(); } } }
The output:
The example of day name by user input
- In the following example, the user is asked to enter the number of day from 1 to 7.
- The value 1 is for Monday and 7 for Sunday.
- Upon entering a number and pressing enter, the program displays the respective day name e.g. Sunday, Monday, and so on.
- If the user enters any number beyond 7, the default case executes that displays the message “Please enter a number between 1 – 7”.
The code:
using System; namespace csharp_example_switch { class Program { static void Main(string[] args) { Console.WriteLine("What is your number for day? (1 - 7)?"); int num_day = int.Parse(Console.ReadLine()); switch (num_day) { case 1: { Console.WriteLine("This is Monday!"); break; } case 2: { Console.WriteLine("This is Tuesday!"); break; } case 3: { Console.WriteLine("This is Wednesday!"); break; } case 4: { Console.WriteLine("This is Thursday!"); break; } case 5: { Console.WriteLine("This is Friday!"); break; } case 6: { Console.WriteLine("This is Saturday!"); break; } case 7: { Console.WriteLine("This is Sunday!"); break; } default: { Console.WriteLine("Please enter a number between 1 – 7"); break; } } Console.ReadLine(); } } }
The output as I entered value 5:
Using string as an expression in the switch statement example
In this example, I used string expression in the switch case statement of C#.
The same example as I used in the introductory part i.e. color names are tested and the corresponding message is displayed for each color (case).
The code:
using System; namespace csharp_switch { class Program { static void Main(string[] args) { string str_col = "Yellow"; switch (str_col) { case "Blue": { Console.WriteLine("Blue Sky"); break; } case "Pink": { Console.WriteLine("Ladies Color!"); break; } case "Brown": { Console.WriteLine("The Potato Chips!"); break; } case "Yellow": { Console.WriteLine("The Mango!"); break; } } Console.ReadLine(); } } }
As the assigned value to the variable is Yellow, so following is the output of this code:
An example of using ENUM in switch case
As mentioned earlier, you may also use the ENUM as an expression in the switch case statement.
In the example below, an ENUM is created with four constants and then used in the switch statement as shown below with the output:
The code with C# Enum:
using System; public enum NEWS { North, East, West, South } public class Example { public static void Main() { NEWS n = (NEWS)(new Random()).Next(2, 3); switch (n) { case NEWS.North: Console.WriteLine("The direction is towards North"); break; case NEWS.East: Console.WriteLine("The direction is towards East"); break; case NEWS.West: Console.WriteLine("The direction is towards West"); break; case NEWS.South: Console.WriteLine("The direction is towards South"); break; default: Console.WriteLine("Directionless"); break; } Console.ReadLine(); } }
The output of the above example: