Answers for "new switch statement c#"

C#
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
0

Switch C#

using System;

public class Example
{
   public static void Main()
   {
      int caseSwitch = 1;
      
      switch (caseSwitch)//switch (比對的運算式)
      {
          case 1://狀況一走這個
              Console.WriteLine("Case 1");
              break;
          case 2:
          case 3://狀況二、三走這個
              Console.WriteLine("Case 2或3");
              break;
          default://以上都不符合走這個
              Console.WriteLine("Default case");
              break;
      }
   }
}
Posted by: Guest on September-14-2021

C# Answers by Framework

Browse Popular Code Answers by Language