The switch statement in C#

 

A switch statement executes the statements that are associated with the value of a given expression, or a default of statements if no match exists.

 

We can use the switch statement in C# as shown below.

 

class Test

{

        static void Main(string[] args)

        {

            switch (args.Length)

            {

                case 0:

                    Console.WriteLine("No arguments were provided");

                    break;

                case 1:

                    Console.WriteLine("One arguments was provided");

                    break;

                default:

                    Console.WriteLine("{0} arguments were provided");

                    break;

            }

        }

}

 

As shown above whenever there are no arguments it displays the "No arguments were provided", if number of arguments are one it displays "One argument was provided" and default it displays the count of arguments provided.