Answers for "c# switch cas"

C#
2

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
Posted by: Guest on December-06-2020
1

c# switch case

// Switch Statement VS Switch Expression
static void TrafficUsingSwitchStatement(string color)
{
    string result;

    switch (color)
    {
        case "Red":
            result = "Stop!"; break;
        case "Yellow":
            result = "Slow down!"; break;
        case "Green":
            result = "Go!"; break;
        default:
            result = "Invalid input!"; break;
    }

    Console.WriteLine(result);
}

static void TrafficUsingSwitchExpression(string color)
{
    string result = color switch
    {
        "Red" => "Stop!",
        "Yellow" => "Slow down!",
        "Green" => "Go!",
        _ => "Invalid input!"
    };

    Console.WriteLine(result);
}
Posted by: Guest on July-17-2021

C# Answers by Framework

Browse Popular Code Answers by Language