Hit enter after type your search item

The switch case statement in C#

The switch case is a decision making statement like the if else statement in C#. The switch statement is used as an alternative (and elegantly 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:

The same can be done by using the if..else if statement as follows:

I will explain what 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#:

Following are a few key points to note about the switch case statement:
  • In C# 6, the expression must return a value as bool, char, string, an integral value like int or long. It may also return an enum value (see an example in the later part).
  • The switch statement is used with the case A switch statement may contain as many case statement as you require. You may provide statements/code to be executed if a certain case is evaluated as True.
  • 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 above example). So, the cases also used string values to be tested for.
  • The break is an optional statement in each case. As the break statement is executed, the switch execution terminates and control moves to the next line after the switch block.
  • If no break statement is used then subsequent cases keep on tested.
  • The default block is also optional. This is like the else statement in if..else statement. If none of the cases is True, the default case executes.

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 have 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 case matches, the default case will execute. Have a look at the code and output as I set the value of the variable as 15:

The output:

The case for value 15 is executed!

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:

The output as I entered value 5:

C# switch

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:

As the assigned value to the variable is Yellow, so following is the output of this code:

The Mango!

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:

The output of the above example:

The direction is towards West

This div height required for enabling the sticky sidebar