how to use variables in a switch statment outisde of the siwtch statment c# code example

Example 1: c# switch

string commandName = "start";

switch (commandName)
{
    case "start":
        Console.WriteLine("Starting service...");
        StartService();
        break;
    case "stop":
        Console.WriteLine("Stopping service...");
        StopService();
        break;
    default:
        Console.WriteLine(String.Format("Unknown command: {0}", commandName));
        break;
}

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