c#.net switch between values code example
Example 1: switch case c# range
int i = 63;
switch (i)
{
case int n when (n >= 100):
Console.WriteLine($"I am 100 or above: {n}");
break;
case int n when (n < 100 && n >= 50 ):
Console.WriteLine($"I am between 99 and 50: {n}");
break;
case int n when (n < 50):
Console.WriteLine($"I am less than 50: {n}");
break;
}
Example 2: c# switch
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;
}