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

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# remove substring 
Csharp :: c# loop class properties add to array 
Csharp :: ActionExecutingContext result response return 
Csharp :: rotate along normal unity 
Csharp :: callling class c# 
Csharp :: how to uncheck a radio button in c# 
Csharp :: c# split large file into chunks 
Csharp :: c# move form without border 
Csharp :: mysql: [Warning] Using a password on the command line interface can be insecure. 
Csharp :: c# export datatatble to excel 
Csharp :: change line color in c# 
Csharp :: distance between two objects unity 2d 
Csharp :: how to have referecne to script in unity 
Csharp :: c# get string in parentheses 
Csharp :: for jump script unity 2d 
Csharp :: c# shorthand if statement without else 
Csharp :: preprocessors in c# 
Csharp :: how to mock http client c# 
Csharp :: properties in c# 
Csharp :: get index brushes properties c# 
Csharp :: predicate EF Core search query 
Csharp :: moving camera with touch screen unity 
Csharp :: How to make enemy shooting 
Csharp :: persian datapicker 
Csharp :: unity apply bloom of a different color 
Csharp :: game maker transparent 
Csharp :: how to round to nearest number in array c# 
Csharp :: unity2d switch camera 
Csharp :: recorrer list c# 
Csharp :: indexing an array 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =