switch case with condition c# code example

Example 1: c# switct case

using System;

public class Example
{
   public static void Main()
   {
      int caseSwitch = 1;
      
      switch (caseSwitch)
      {
          case 1:
              Console.WriteLine("Case 1");
              break;
          case 2:
              Console.WriteLine("Case 2");
              break;
          default:
              Console.WriteLine("Default case");
              break;
      }
   }
}
// The example displays the following output:
//       Case 1

Example 2: switch c#

switch(expression) 
{
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
    break;
}

Example 3: c# switch

using System;

namespace SwitchStatement
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Please write a movie genre.");
      string genre = Console.ReadLine();
      switch (genre)
      {
        case "Drama":
        Console.WriteLine("Citizen Kane");
        break;
        case "Comedy":
        Console.WriteLine("Duck Soup");
        break;
        case "Adventure":
        Console.WriteLine("King Kong");
        break;
        case "Horror":
        Console.WriteLine("Psycho");
        break;
        case "Science Fiction":
        Console.WriteLine("2001: A Space Odyssey");
        break;
        default:
        Console.WriteLine("No movie found.");
        break;
      }
    }
  }
}

Example 4: c# switch

int randomNumber = rnd.Next(1, 2); //a int tat equals 1 or 2

      switch (randomNumber) //the number we check the value of
      {
          case 1:
              Console.WriteLine("randomNumber = 1!");
              break;
          case 2:
              Console.WriteLine("randomNumber = 2!");
              break;
          default:	//the deafult state happens when the other casas do not
              Console.WriteLine("something went wrong");
              break;
      }