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# integer

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 :: append an array in c# 
Csharp :: Metadata publishing for this service is currently disabled 
Csharp :: c# combobox lock edit 
Csharp :: unity get prefabs from folder 
Csharp :: c# get witdh of matrix 
Csharp :: c# dictionary get key by value 
Csharp :: Allow edit in Datagrid C# 
Csharp :: unity list get item at index 
Csharp :: create new object c# 
Csharp :: c# return tuple 
Csharp :: unity camera fade to black 
Csharp :: class in c# 
Csharp :: unity detect when an object has been clicked 
Csharp :: HTTP Error 500.35 - ASP.NET Core does not support multiple apps in the same app pool 
Csharp :: how to see if a number is even c# 
Csharp :: remove scenedelegate 
Csharp :: preprocessors 
Csharp :: c# mvc get current directory 
Csharp :: how to add skybox in unity 
Csharp :: .net json result status code not working 
Csharp :: ASP.net ApplicationUser referance not found 
Csharp :: How do I allow edit only a particular column in datagridview in windows application 
Csharp :: Make UI/Canvas look at Camera always. 
Csharp :: unity DOScale 
Csharp :: telerik winforms get value of selected rows from grid 
Csharp :: multi case in c# 
Csharp :: dictionaries in unity 
Csharp :: wpf keydown detect if control key is down 
Csharp :: length of list c# 
Csharp :: 2d array 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =