xxxxxxxxxx
public Orientation ToOrientation(Direction direction) => direction switch
{
Direction.Up => Orientation.North,
Direction.Right => Orientation.East,
Direction.Down => Orientation.South,
Direction.Left => Orientation.West,
_ => throw new ArgumentOutOfRangeException(nameof(direction), $"Not expected direction value: {direction}"),
};
xxxxxxxxxx
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;
}
xxxxxxxxxx
// 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;
}
}
xxxxxxxxxx
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
xxxxxxxxxx
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;
}
xxxxxxxxxx
// 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;
}
}
xxxxxxxxxx
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;
}
xxxxxxxxxx
// 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;
}
xxxxxxxxxx
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;
}
xxxxxxxxxx
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;
}
xxxxxxxxxx
string command = "stop";
switch(command){
case "start" :
Console.WriteLine("started your alexa");
break;
case "stop":
Console.WriteLine("stopped your alexa");
break;