switch case condition c# code example

Example 1: c# switch

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: 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;
      }