c# switch case enum code example

Example 1: enum switch menu c#

enum Menu
        {
            Students_Detail = 1,
            Display = 2,
            Exit = 3
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Please select a menu option");
            Console.WriteLine("1. Students Detail");
            Console.WriteLine("2. Display");
            Console.WriteLine("3. Exit");
            int pick = Convert.ToInt32(Console.ReadLine());
            Menu menuChoice = new Menu();
            menuChoice = (Menu)pick;

            switch (pick)
            {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                default:
                    break;
            }
        }

Example 2: c# switch case

public class Example
{
  // Button click event
  public void Click(object sender, RoutedEventArgs e)
  {
            if (sender is Button handler)
            {
                switch (handler.Tag.ToString())
                {
                  case string tag when tag.StartsWith("Example"):
                       // your code
                    break;
                    
                  default:
                    break;
                }
            }
  }
}

Example 3: c# switch case enum

public enum Operator
{
    PLUS, MINUS, MULTIPLY, DIVIDE
}


switch(op)
{
     case Operator.PLUS:
     {
        // your code 
        // for plus operator
        break;
     }
     case Operator.MULTIPLY:
     {
        // your code 
        // for MULTIPLY operator
        break;
     }
     default: break;
}