// 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;
}
}
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
//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;
}
// 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;
}
}
// 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;
}
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;
}
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;
}
string command = "stop";
switch(command){
case "start" :
Console.WriteLine("started your alexa");
break;
case "stop":
Console.WriteLine("stopped your alexa");
break;
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;
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;
}
}
}
}
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",
_ => "",
};
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));
}
/*
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);
// 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;
}
}
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
//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;
}
// 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;
}
}
// 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;
}
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;
}
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;
}
string command = "stop";
switch(command){
case "start" :
Console.WriteLine("started your alexa");
break;
case "stop":
Console.WriteLine("stopped your alexa");
break;
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;
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;
}
}
}
}
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",
_ => "",
};
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));
}
/*
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);