Switch Case in C#

Hi Programmers , In this article i’ll write definition and program for switch case in c# console application. The Switch case is selection statement that chooses a single switch section to execute from a list of options based on a pattern
match with the match expression. we can pass char,string,bool,integral value, int or long, an enum value etc. let’s see the codes.

Example 1 : Using Integer Data Type
Example 2 : Using Integer Data Type with user input
Example 3 : Directly passing value into switch case.
Example 4 : Using Character Data Type
Example 5 : Using Boolean Data Type
Example 6 : Using Enum

Directly Test Below Codes into Editor.

Example 1 : Using Integer Data Type
using System;
namespace ConsoleApp1 {
    class Program {         
        
        static void Main(string[] args) 
        {
            int i = 4;
            switch(i)
            {
                case 1: Console.WriteLine("Case 1 Executed "); break;

                case 2: Console.WriteLine("Case 2 Executed "); break;

                case 3: Console.WriteLine("Case 3 Executed "); break;

                case 4: Console.WriteLine("Case 4 Executed "); break;

                default: Console.WriteLine("Default Case Executed "); break;

            }
        }
    }
}

***********************************
Example 2 : Using Integer Data Type with user input
using System;
namespace ConsoleApp1 {
    class Program {         
        
        static void Main(string[] args) 
        {
            Console.WriteLine("Enter Any Number :");
            int i = int.Parse(Console.ReadLine());
            switch(i)
            {
                case 1: Console.WriteLine("Case 1 Executed "); break;

                case 2: Console.WriteLine("Case 2 Executed "); break;

                case 3: Console.WriteLine("Case 3 Executed "); break;

                case 4: Console.WriteLine("Case 4 Executed "); break;

                default: Console.WriteLine("Default Case Executed "); break;

            }
        }
    }
}

********************************
Example 3 : Directly passing value into switch case.
using System;
namespace ConsoleApp1 {
    class Program {         
        
        static void Main(string[] args) 
        {
            switch(3)
            {
                case 1: Console.WriteLine("Case 1 Executed "); break;

                case 2: Console.WriteLine("Case 2 Executed "); break;

                case 3: Console.WriteLine("Case 3 Executed "); break;

                case 4: Console.WriteLine("Case 4 Executed "); break;

                default: Console.WriteLine("Default Case Executed "); break;

            }
        }
    }
}

***********************
Example 4 : Using Character Data Type
using System;
namespace ConsoleApp1 {
    class Program {         
        
        static void Main(string[] args) 
        {
            switch('4')
            {
                case '1': Console.WriteLine("Case 1 Executed "); break;

                case '2': Console.WriteLine("Case 2 Executed "); break;

                case '3': Console.WriteLine("Case 3 Executed "); break;

                case '4': Console.WriteLine("Case 4 Executed "); break;

                default: Console.WriteLine("Default Case Executed "); break;

            }
        }
    }
}

**************************
Example 5 : Using Boolean Data Type
using System;
namespace ConsoleApp1 {
    class Program {         
        
        static void Main(string[] args) 
        {
            bool b = true;
            switch(b)
            {
                case true: Console.WriteLine("True Case Executed "); break;

                case false: Console.WriteLine("False Case Executed "); break;
            }
        }
    }
}

*******************************
Example 6 : Using Enum

using System;
namespace ConsoleApp1 {
    class Program {
        public enum Emp { Smith, Sam, Sean }
        static void Main(string[] args)
        {
            Emp e = (Emp)(new Random()).Next(0, 3);
            switch (e)
            {
                case Emp.Smith:
                    Console.WriteLine("The Employee Smith");
                    break;
                case Emp.Sam:
                    Console.WriteLine("The Employee Sam");
                    break;
                case Emp.Sean:
                    Console.WriteLine("The Employee Sean");
                    break;
                default:
                    Console.WriteLine("The Employee Not Exist");
                    break;
            }
        }       
    }
}

Happy Coding…Thanks

Post Author: adama