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
string command = "stop";
switch(command){
case "start" :
Console.WriteLine("started your alexa");
break;
case "stop":
Console.WriteLine("stopped your alexa");
break;
xxxxxxxxxx
int number = 1;
switch (number)
{
case 1:
Console.WriteLine("One");
break;
case 2:
Console.WriteLine("Two");
break;
default:
Console.WriteLine("Other number");
break;
}