In this article I am going to discuss about the C# Switch Statement in detail but before that if you had not read my previous article of C# Else-If Statement then must read it. In C# Programming we had studied if-else statements and also studied else-if statement and when to use this statements but is this always a good approach? The answer is no. In C# we use else-if statement only if we have maximum 4-5 conditions but if we have more than 5 conditions then it is not a good practice to use else-if statements then for solving that problem switch statement comes into existence.

Introduction to C# Switch Statement

In C# Switch Statement is one of the selection statement in the control flow statements. Switch statement is one of the alternative for if else-if statement. In C#, the Switch statement is a multiway branch statement. It provides an very good way to transfer the execution to different piece of code based on value of the expression. The switch expression is of any type which includes integer, character, strings and enums. In switch statement the expression is checked for different cases and the match case will be executed.

Why we need Switch Statement?

As I already told, Switch Statements are one of the alternative to avoid long if else-if statements. In C#, switch is a keyword(reserved word) and using this keyword we can create selection statements for multiple blocks and each block is constructed using case keyword. So when we have multiple options and we only have to choose one condition among others then we need to go for switch statement because as per selected option it executes the piece of code. The syntax for switch statement is given below:

switch(expression) 
{
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default://when any of the case(condition) not met then this default block is              executed like the else block in if-else statement.
    // code block
    break;
}

C# Program to Explain Switch Statement

using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 2;
            switch (number)
            {
                case 1:  //here we check if case is equal to number
                    Console.WriteLine("Number is 1");
                    break;
                case 2:
                    Console.WriteLine("Number is 2");
                    break;
                case 3:
                    Console.WriteLine("Number is 3");
                    break;
                default:
                    Console.WriteLine("Number other than 1, 2 and 3");
                    break;
            }
            Console.ReadKey();
        }
    }
}
Output:
Number is 2

In above code you have noticed that we have used break keyword which is type of jump statement. We have used break statement to come out of the switch statement. If we don’t use break statement then it will throw compilation error.

Is it Mandatory to use Default Block?

No, Default Block is not necessary in switch statement. As I already told default block acts like else block of if-else statement. In switch statement when any of the condition not met then default block is executed but if we have not put the default block in our switch statement then no block will be executed inside switch statement.

Switch Statement with Enums in C#

We can also use C# Switch Statement with enums to check for the cases. For implementing Switch statement with enums first we have to make one enum then we have to pass the enum to switch keyword and then we have to check for enum cases. If you don’t know about enum then don’t worry we will discuss the enums in detail in our next articles. But for now you can understand that enum is user defined datatype that is used to storing any type of data. Let see an example to understand switch statement with enums.

public enum Color //creating enum of color
{
    Red,
    Green,
    Blue,
}
using System;
namespace SwitchStatementWithEnums
{
    class Program
    {
        static void Main(string[] args)
        {
            Color name=Color.Red;  //how to access enum value
            switch (name)
            {
                case Color.Red:
                    Console.WriteLine("Color is Red");
                    break;
                case Color.Blue:
                    Console.WriteLine("Color is Blue");
                    break;
                case Color.Green:
                    Console.WriteLine("Color is Green");
                    break;
            }
            Console.ReadKey();
        }
    }
}
Output:
Color is Red

Nested Switch Statement

In C# (csharp) we can also use the nested switch statement in our program. Nested Switch Statement means we can use another switch statement inside one switch statement. But this will not always a good practice to use nested switch statement but sometimes we need to implement nesting in our program. For example:

using System;
namespace ControlFlowDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a Number Between 1 and 3:");
            int number = Convert.ToInt32(Console.ReadLine());

            //outer Switch Statement
            switch (number)
            {
                case 1:
                    Console.WriteLine("You have Entered One");
                    Console.Write("Choose Color Code (R/G/B): ");
                    char color = Convert.ToChar(Console.ReadLine());

                    //Inner Switch Statement
                    switch (Char.ToUpper(color))
                    {
                        case 'R':
                            Console.WriteLine("You have Selected Red Color");
                            break;
                        case 'G':
                            Console.WriteLine("You have Selected Green Color");
                            break;
                        case 'B':
                            Console.WriteLine("You have Selected Blue Color");
                            break;
                        default:
                            Console.WriteLine($"You Have Enter Invalid Color Code: {Char.ToUpper(color)}");
                            break;
                    }
                    break;

                case 2:
                    Console.WriteLine("You have Entered Two");
                    break;

                case 3:
                    Console.WriteLine("You have Entered Three");
                    break;
                default:
                    Console.WriteLine("Please Enter number between 1 to 3.");
                    break;
            }

            Console.ReadLine();
        }
    }
}
Output:
Enter a Number Between 1 and 3:1
You have Entered One
Choose Color Code (R/G/B): b
You have Selected Blue Color

Conclusion

In this C# tutorial article I had discussed C# Switch Statement with example in very detail. Instead of this I had also discussed how we can use the switch statement with enums and how to use nested switch statement in our program. Hope you like this article of csharp switch statement. In my next article I discuss about the iteration statement.