Switch in C#


        The switch statement enables you to select from multiple choices based on a set of fixed values for a given expression.

In the switch statement, the selection is determined by the value of an expression that you specify.   If the value of the switch expression does not match any of the case values, the switch automatically selects the default case.

Example program using do while in c# and switch in c#

C# Switch Example


using System;
 
namespace switchincs
{
    class switchexample
    {
        static void Main(string[] args)
        {
            int choice = 0;
            Console.WriteLine(" 1 - I like Foot ball\n 2 - I like Cricket\n 3 - I like Tennis");
 
            Console.WriteLine("Please select your choice between 1 and 3");
            choice = int.Parse(Console.ReadLine());
 
            switch (choice)
            {
                case 1:
                    Console.WriteLine("You like Foot ball");
                    break;
                case 2:
                    Console.WriteLine("You like Cricket");
                    break;
                case 3:
                    Console.WriteLine("You like Tennis");
                    break;
                default:
                    Console.WriteLine("Please select between 1 and 3");
                    break;
            }  
            Console.Read(); //To make console visible after processing.
        }
    }
}
Output

1 - I like Foot ball
 2 - I like Cricket
 3 - I like Tennis
Please select your choice between 1 and 3
2
You like Cricket



switch example c# switch example c# switch syntax c# switch example c# case c# switch case switch case c# switch statement in c examples switch statement examples C#.NET Switch.