C# Switch

Last Updated on May 10, 2022
C# Switch

C# Switch

The switch statement is to check and execute the block of code from multiple conditions in C#.

Let's take a look at some of the examples of the switch statement.

 
//C# switch code syntax

switch(expression){    
    case condtion1:    
        //code to be executed
        break;  
    case condtion2:    
        //code to be executed
        break;        
    default:     
        //code to be executed if any of the above cases are not matched 
        break;  
}    

C# code example.

 
using System;
                
public class Program
{
    public static void Main()
    {
        int nMonth = 5;
        switch(nMonth)
        {
            case 1:
                Console.WriteLine("January");
            break;
            case 2:
                Console.WriteLine("February");
            break;
            case 3:
                Console.WriteLine("March");
            break;
            case 4:
                Console.WriteLine("April");
            break;
            case 5:
                Console.WriteLine("May");
            break;
            case 6:
                Console.WriteLine("June");
            break;
            case 7:
                Console.WriteLine("July");
            break;
            case 8:
                Console.WriteLine("August");
            break;
            case 9:
                Console.WriteLine("September");
            break;
            case 10:
                Console.WriteLine("October");
            break;
            case 11:
                Console.WriteLine("November");
            break;
            case 12:
                Console.WriteLine("December");
            break;
        }
        
        //Output: May
    }
}

break keyword has been used to break out from the switch code block and it is always important to include the break keyword.

C# code example with using default keyword. The default keyword is optional to execute the default code statement if any case doesn't match.

 
using System;
                
public class Program
{
    public static void Main()
    {
        int nMonth = 13;
        switch(nMonth)
        {
            case 1:
                Console.WriteLine("January");
            break;
            case 2:
                Console.WriteLine("February");
            break;
            case 3:
                Console.WriteLine("March");
            break;
            case 4:
                Console.WriteLine("April");
            break;
            case 5:
                Console.WriteLine("May");
            break;
            case 6:
                Console.WriteLine("June");
            break;
            case 7:
                Console.WriteLine("July");
            break;
            case 8:
                Console.WriteLine("August");
            break;
            case 9:
                Console.WriteLine("September");
            break;
            case 10:
                Console.WriteLine("October");
            break;
            case 11:
                Console.WriteLine("November");
            break;
            case 12:
                Console.WriteLine("December");
            break;
            default:
               Console.WriteLine("No month found!");
            break;
        }
        
        //Output: No month found!
    }
}