Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# switch statement

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

switch expression c#

//called: switch expressions

int number= 2;

string result = number switch
{
    0 => "zero",
    1 => "one",
    2 => "two",
    _ => "default"
};

Console.WriteLine(result);
Comment

c# switch

// switch..case with enum
void WeekEndOrWeekDay()
{
   switch (DateTime.Now.DayOfWeek)
   {
   case DayOfWeek.Saturday:
   case DayOfWeek.Sunday:
      Console.WriteLine("Today is Weekend");
   break;
   default:
      Console.WriteLine("Today is a work day.");
   break;
   }
}
Comment

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
Comment

switch c#

//EXAMPLE
int day = 4;
switch (day) 
{
  case 1:
    Console.WriteLine("Monday");
    break;
  case 2:
    Console.WriteLine("Tuesday");
    break;
  case 3:
    Console.WriteLine("Wednesday");
    break;
  case 4:
    Console.WriteLine("Thursday");
    break;
  case 5:
    Console.WriteLine("Friday");
    break;
  case 6:
    Console.WriteLine("Saturday");
    break;
default:
    Console.WriteLine("Sunday");
break;
}
Comment

c# switch

// switch..case with string
void StringSwitchCase()
{
   string name = "Mahesh";
   switch (name)
   {
   case "Mahesh":
      Console.WriteLine("First name was used!");
   break;
   case "Chand":
      Console.WriteLine("Last name was used!");
   break;
   default:
      Console.WriteLine("No name found!");
   break;
   }
}
Comment

c# switch

// Generate a random value between 1 and 9
int caseSwitch = new Random().Next(1, 9);
switch (caseSwitch)
{
   case 1:
      Console.WriteLine("Case 1");
   break;
   case 2:
      Console.WriteLine("Case 2");
   break;
   case 3:
      Console.WriteLine("Case 3");
   break;
   default:
      Console.WriteLine("Value didn't match earlier.");
   break;
}
Comment

c# switch

switch (caseSwitch)
{
   case 1:
      Console.WriteLine("Case 1");
      DateTime date = DateTime.Today;
      Console.WriteLine("Today's date is {0}", date);
      if (date.Day == 2)
      {
         Console.WriteLine("This is the shortest month");
      }
   break;
   case 2:
      Console.WriteLine("Case 2");
   break;
   case 3:
      Console.WriteLine("Case 3");
   break;
   default:
      Console.WriteLine("Default case");
   break;
}
Comment

switch case c#

int value = 0;
switch (value) {
  case 0:
    // do something
    break;
  case 1: 
    // do something else
    break;
   
  default :
  	// something if anything not match
}
Comment

c# switch

public enum Color { Red, Green, Blue, Black, Orange }
public static void RandomConsoleBackground()
{
   Color c = (Color)(new Random()).Next(0, 4);
   switch (c)
   {
   case Color.Red:
      Console.BackgroundColor = ConsoleColor.Red;
      Console.Clear();
      Console.WriteLine("Red");
   break;
   case Color.Green:
      Console.BackgroundColor = ConsoleColor.Green;
      Console.Clear();
      Console.WriteLine("Green");
   break;
   case Color.Blue:
   case Color.Black:
   case Color.Orange:
   default:
      Console.WriteLine("No need to change background.");
   break;
}
Comment

switch c#

switch(expression) 
{
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
    break;
}
Comment

c# switch

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
    break;
}
Comment

c# swtich

 switch (caseSwitch)
      {
          case 1:
              Console.WriteLine("Case 1");
              break;
          case 2:
              Console.WriteLine("Case 2");
              break;
          default:
              Console.WriteLine("Default case");
              break;
Comment

switch expression c#

var switchValue = 3;
var resultText = switchValue switch
{
    1 or 2 or 3 => "one, two, or three",
    4 => "four",
    5 => "five",
    _ => "unknown",
};
Comment

switch c#

int something = 2;

switch(something)
{
  case 1:
    Console.WriteLine(1);
    break;
  case 2:
    Console.WriteLine(2);
    break;
}
Comment

c# switch case with or condition

int i = 5;
switch (i)
{
    case(1):
    case(2):
        Console.WriteLine(i);
        break;
    default:
        break;
}
Comment

switch c#

var caseSwitch = 1;
switch (caseSwitch)
  {
    case 1:
      Console.WriteLine("Case 1");
      break;
    case 2:
      Console.WriteLine("Case 2");
      break;
    default:
      Console.WriteLine("Default case");
      break;
  }
Comment

c# switch statement

string command = "stop";
switch(command){
  case "start" :
    Console.WriteLine("started your alexa");
    break;
  case "stop":
	Console.WriteLine("stopped your alexa");
	break;
Comment

switch case C#

int Length = mystring.Length;
int range = (Length - 1) / 25;
switch (range)
{
    case 0:
        Console.WriteLine("Range between 0 to 25");
        break;
    case 1:
        Console.WriteLine("Range between 26 to 50");
        break;
    case 2:
        Console.WriteLine("Range between 51 to 75");
        break;
Comment

C# Switch and case

int number = 2;

switch(number)
{
	case 1:
		Console.WriteLine("Number is 1");
		break;
	case 2:
		Console.WriteLine("Number is 2");
		break;
}
Comment

C# Switch Statements

Console.WriteLine(GetDay(5));
Console.ReadLine();


static string GetDay(int dayNum)
{
    string dayName;

    switch (dayNum)
    {
        case 0:
            dayName = "Sunday";
                break;
       case 1:
            dayName = "Monday";
            break;
       case 2:
            dayName = "Tuesday";
            break;
        case 3:
            dayName = "Wednesday";
            break;
        case 4:
            dayName = "Thursday";
            break;
        case 5:
            dayName = "Friday";
            break;
        case 6: dayName = "Saturday";
                break;
        default:
            dayName = "Invalid Day Number";
            break;
    }

    return dayName;

}
Comment

c# switch case

switch (caseSwitch)     // Match Expression - can be any non-null expression
{
    case 1:             // Case Label 1    Switch Section START
    case 2:             // Case Label 2
        // ...
        break;          //                 Switch Section END
    case 3:             // Case Label 3    Switch Section START
        // ...
        break;          //                 Switch Section END
    default:            // Default Label   Switch Section START
        // ...
        break;          //                 Switch Section END
}

switch (caseSwitch)
{
    ...
    case TypeA myVar when myVar.Size > 0:
        ...
        break;
    case <type> <variable_name> when <any_boolean_expression>:
        ...
        break;
    ...
}
Comment

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

c# switch

scale = exponent switch
        {
            int n when (n >= 6 && n < 9) => "Million",
            int n when (n >= 9 && n < 12) => "Billion",
            int n when (n >= 12 && n < 15) => "Trillion",
            int n when (n >= 15 && n < 18) => "Quadrillion",
            int n when (n >= 18 && n < 21) => "Quintillion",
            int n when (n >= 21 && n < 24) => "Sextillion",
            int n when (n >= 24 && n < 27) => "Septillion",
            int n when (n >= 27 && n < 30) => "Octillion",
            30 => "Nonillion",
            _ => "",
        };
Comment

c# switch

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

c# switch case


switch(shape)
{
    case Circle c:
        WriteLine($"circle with radius {c.Radius}");
        break;
    case Rectangle s when (s.Length == s.Height):
        WriteLine($"{s.Length} x {s.Height} square");
        break;
    case Rectangle r:
        WriteLine($"{r.Length} x {r.Height} rectangle");
        break;
    default:
        WriteLine("<unknown shape>");
        break;
    case null:
        throw new ArgumentNullException(nameof(shape));
}

Comment

c# switch when

/*
	Why use many "if" statements if you can just use a switch
	and clean alot of your code!

	Below are two ways to make your life alot easier!
*/

// Using a traditional switch statement

string test = "1";
switch (test)
{
	case "*":
		Console.WriteLine("test");
		break;

	case "Something else":
		Console.WriteLine("test1");
		break;

	case string when test != "*":
		Console.WriteLine("test2");
		break;

  	default:
	Console.WriteLine("default");
		break;
}

// Using a switch expression
// This obviously results in much cleaner code!

string result = test switch
{
	"*" => "test",
	"test" => "test1",
	string when test != "*" => "test2",
	_ => "default" // In switch expressions the _ is the same as default
};

Console.WriteLine(result);
Comment

c# switch case set value

int x = SwitchExpressionUsingGuardClause(y);

// Keywords: Switch Expressions, C# 8.0, Guard Clause
private int SwitchExpressionUsingGuardClause(int y)
{
    switch (y)
    {
        case 0: return 10;
        case 1: return 20;
        default: return 5;
    }
}
Comment

c# switch example

switch (iGender)
            {
                case 1:
                    LabelOne.Text = "Male";
                    break;

                case 2:
                    LabelOne.Text = "Female";
                    break;
Comment

c# switch

switch (expression)
{
   case expression_value1:
      Statement
   break;
   case expression_value2:
      Statement
   break;
   case expression_value3:
         Statement
   break;
   default:
      Statement
   break;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# picturebox transparente 
Csharp :: how to define a function in c# 
Csharp :: how to close a form c# 
Csharp :: get unique array based on value in c# 
Csharp :: c# string to int 
Csharp :: c# array display 
Csharp :: c# handle dbnull value 
Csharp :: how to upload an image to an image source c# 
Csharp :: c# regex find last match 
Csharp :: static c# 
Csharp :: how to make player movement in unity 2d 
Csharp :: mvc session key exists 
Csharp :: Sort ListBox numerically in C# 
Csharp :: draw on picturebox c# 
Csharp :: c# api bypass ssl certificate 
Csharp :: c# func 
Csharp :: authentication and authorization in asp.net c# with example 
Csharp :: how to show process time run c# 
Csharp :: C# extract all of a property from a list of objcets 
Csharp :: on collision unity 
Csharp :: remove last instance of string c# 
Csharp :: what is list in c# 
Csharp :: c# list find index 
Csharp :: c# string 
Csharp :: bytes size c# 
Csharp :: C# xamaring form change text on label 
Csharp :: preprocessors in c# 
Csharp :: c# null conditional 
Csharp :: C# How to make a field read-only outside of class 
Csharp :: dataGridView default error dialog handle 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =