In C#, the Switch statement is the type of control flow statement to compare a single expression with multiple values. If a value matches the test expression, then the specific block of code is executed. If the case does not match the expression, the compiler jumps to the next case. The purpose of the switch case statements is to simplify the code that requires multiple if-else statements. 

The purpose of this guide is to elaborate on the syntax and examples of the switch statement in C#.

How C# Switch works

The basic syntax of the switch statements in C# is as: 

switch (exp)
{
     case <val1>;
     <Statement(s) of C# code>
     break;
     case <val2>;
     <Statement(s) of C# code>
     break;
     default;
     <Statement(s) of C# code>
     break;      
}

The description of the syntax is:

  • The expression is defined after the “switch” keyword as shown above. The “exp” can be a variable or any condition, i.e., integer, string, or condition.
  • The main body defines the cases using the “case” keyword. After each case, the statements of the C# code are executed. 
  • After the statements, the “break” keyword occurs, which breaks the flow if that specific case matches the “exp”.
  • Once all the statements are matched, then the “default” comes into play which also carries some code statements. This piece of code is executed if none of the cases matches the “exp” value.

Note: There could be multiple case statements as per the requirement of the situation

Examples of Switch Case Statement in C#

Here, a few examples are explained to demonstrate the usage of the switch case statement in C#. 

Example 1: Switch Case on a Numeric Value

As discussed earlier, the expression can be an integer, string, float, char, etc. In this example, the “expression” will be a numeric value, and the cases will be matched in each case. 

using System;
    public class NVBlog {
       public static void Main(string[] args)  {
            int z = 7;
            switch (z)
            {
            case 1:
                Console.WriteLine("The Value is True");
            break;
            case 3:
                Console.WriteLine("The Value is 3");
            break;
            case 5:
                Console.WriteLine("The Value is 5");
            break;
            case 7:
                Console.WriteLine("The Value is 7");
            break;
        }    
    }
} 

In the code:

  • An integer is initialized “z=7” and used as an expression in the switch.
  • There are a total of four cases, i.e., case 1, case 3, case 5, and case 7. The compiler checks each case and continues to the other until it matches “case 7”.

Output

switch statement in c sharp

The statement matching “case 7“ has been printed.

Example 2: The default Case in the Switch Statement 

Now, let’s understand the functionality of the default case in the switch statement. 

using System;
    public class NVBlog {
        public static void Main(string[] args)  {
            String t = "Naveed Ul Haq";
            switch (t)
            {
            case "Naveed":
                Console.WriteLine("The expression's value is Naveed");
            break;
            case "Ul Haq":
                Console.WriteLine("The expression's value is Ul Haq");
            break;
            default:
                Console.WriteLine("Hey! Welcome to Naveed Ul Haq Blog");
            break;
        }    
    }
}

The code’s explanation is:

  • A string variable is initialized to be used as an expression in the “switch”.
  • Two cases containing the values “Naveed” and “Ul Haq‘ are checked.
  • Lastly, there is a print statement inside the “default” keyword to be used if none of the cases matches the switch expression.
switch statement in c sharp

As the output shows that the code statement inside the “default” case is printed on the console.

Wrap up 

In C# for any programming language, switch statements can make code easier to read and understand, especially when dealing with a large number of possible values. By the end, you have learned the purpose of the switch case statement with the help of various examples.

If you want to learn how to format strings in C#, please look at my blog on C# String Format

Categorized in: