C# Switch Case Statement with Examples

In c#, Switch is a selection statement and it will execute a single case statement from the list of multiple case statements based on the pattern match with the defined expression.

 

By using switch statement in c#, we can replace the functionality of if…else if statement to provide a better readability for the code.

Syntax of C# Switch Statement

Generally, in c# switch statement is a collection of multiple case statements and it will execute only one single case statement based on the matching value of expression.

 

Following is the syntax of defining the switch statement in c# programming language.

 

switch(variable/expresison){

case value1:

// Statements to Execute

break;

case value2:

//Statements to Execute

break;

....

....

default:

// Statements to Execute if No Case Matches

break;

}

If you observe above syntax, we defined a switch statement with mutiple case statements. Here, the switch statement will evaluates the expression / variable value by matching with the case statement values (value1, value2, etc.). If variable / expression value matches with any case statement, then the statements inside of particular case will be executed.

 

In case, if none of the case statements are matches with the defined expression / variable value, then the statements inside of default block will be executed and it’s more like else block in if...else statement.

 

If you observe above code, we used a break keyword at the end of each case statement to stop the further execution of non-matching case statements in switch.

C# Switch Statement Flow Chart Diagram

Following is the pictorial representation of switch case statement process flow in c# programming language.

 

C# Switch Case Statement Flow Chart Diagram

 

If you observe above switch statement flow chart, the process flow of switch statement will starts from Top to Bottom and in first case it will check whether the expression value matching or not.

 

In case, the expression value matches means it will execute the particular case statements block and exist the switch statement otherwise it will go to second case statement and check whether expression value matching or not, same way search will continue till it find right case statement.

 

In case, if all case statements are failed to match with the defined expression value, then the default block statements will be executed and switch statement will comes to an end.

C# Switch Case Statement Example

Following is the example of using switch statement in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 20;

            switch (x)

            {

                case 10:

                    Console.WriteLine("x value is 10");

                    break;

                case 15:

                    Console.WriteLine("x value is 15");

                    break;

                case 20:

                    Console.WriteLine("x value is 20");

                    break;

                default:

                    Console.WriteLine("Not Known");

                    break;

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined a switch with multiple case statements and it will execute the matched case statements with the expression value.

 

When we run above c# program, we will get the result like as shown below.

 

C# Switch Case Statement Example Result

 

If you observe above result, the case statement (20) matches with the defined expression value (20) and executed the statements within the respective case statement.

C# Nested Switch Case Statements

In c#, using one switch statement within another switch statement is called a nested switch case statements.

 

Following is the example of using nested switch statements in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x = 10, y = 5;

            switch (x)

            {

                case 10:

                    Console.WriteLine("X Value: 10");

                    switch(y){

                        case 5:

                            Console.WriteLine("Nested Switch Value: 5");

                            switch (y - 2) {

                                case 3:

                                    Console.WriteLine("Another Nested Switch Value: 3");

                                    break;

                            }

                            break;

                    }

                    break;

                case 15:

                    Console.WriteLine("X Value: 15");

                    break;

                case 20:

                    Console.WriteLine("X Value: 20");

                    break;

                default:

                    Console.WriteLine("Not Known");

                    break;

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used switch statements within another switch statements to implement nested switch statements based on our requirements.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Nested Switch Case Statement Example Result

 

If you observe above result, the nested switch statements has been executed based on our requirements.

C# Switch Case Statement with Enum

In c#, we can use enum values with Switch case statements to perform required operations.

 

Following is the example of using enum values in c# switch case statement.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            location loc = location.hyderabad;

            switch (loc)

            {

                case location.chennai:

                    Console.WriteLine("Location: Chennai");

                    break;

                case location.guntur:

                    Console.WriteLine("Location: Guntur");

                    break;

                case location.hyderabad:

                    Console.WriteLine("Location: Hyderabad");

                    break;

                default:

                    Console.WriteLine("Not Known");

                    break;

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public enum location

        {

            hyderabad,

            chennai,

            guntur

        }

    }

}

If you observe above example, we defined a enum values and using those values in switch case statements to perform required operations based on our requirements.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Switch Case Statement with Enums Example Result

 

If you observe above result, the switch case statement which matches with enum value has been printed in console window.

 

This is how we can use enums with switch case statements to perform an operations based on our requirements.