Answers for "switch case c# example"

C#
1

c# switch statement

string command = "stop";
switch(command){
  case "start" :
    Console.WriteLine("started your alexa");
    break;
  case "stop":
	Console.WriteLine("stopped your alexa");
	break;
Posted by: Guest on October-30-2021
5

c# select 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
Posted by: Guest on May-29-2020
5

switch expression c#

var switchValue = 3;
var resultText = switchValue switch
{
    1 or 2 or 3 => "one, two, or three",
    4 => "four",
    5 => "five",
    _ => "unknown",
};
Posted by: Guest on March-07-2021
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#

switch(expression) 
{
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
    break;
}
Posted by: Guest on November-26-2020
2

c# switch case

public class Example
{
  // Button click event
  public void Click(object sender, RoutedEventArgs e)
  {
            if (sender is Button handler)
            {
                switch (handler.Tag.ToString())
                {
                  case string tag when tag.StartsWith("Example"):
                       // your code
                    break;
                    
                  default:
                    break;
                }
            }
  }
}
Posted by: Guest on August-27-2020

C# Answers by Framework

Browse Popular Code Answers by Language