// switch..case with enumvoidWeekEndOrWeekDay(){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;}}
usingSystem;publicclassExample{publicstaticvoidMain(){int caseSwitch =1;switch(caseSwitch){case1:
Console.WriteLine("Case 1");break;case2:
Console.WriteLine("Case 2");break;default:
Console.WriteLine("Default case");break;}}}// The example displays the following output:// Case 1
// switch..case with stringvoidStringSwitchCase(){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 9int caseSwitch =newRandom().Next(1,9);switch(caseSwitch){case1:
Console.WriteLine("Case 1");break;case2:
Console.WriteLine("Case 2");break;case3:
Console.WriteLine("Case 3");break;default:
Console.WriteLine("Value didn't match earlier.");break;}
switch(caseSwitch){case1:
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;case2:
Console.WriteLine("Case 2");break;case3:
Console.WriteLine("Case 3");break;default:
Console.WriteLine("Default case");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){case0:
Console.WriteLine("Range between 0 to 25");break;case1:
Console.WriteLine("Range between 26 to 50");break;case2:
Console.WriteLine("Range between 51 to 75");break;
publicclassExample{// Button click eventpublicvoidClick(object sender,RoutedEventArgs e){if(sender isButton handler){switch(handler.Tag.ToString()){casestring tag when tag.StartsWith("Example"):// your codebreak;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){caseCircle c:WriteLine($"circle with radius {c.Radius}");break;caseRectangle s when(s.Length == s.Height):WriteLine($"{s.Length} x {s.Height} square");break;caseRectangle r:WriteLine($"{r.Length} x {r.Height} rectangle");break;default:WriteLine("<unknown shape>");break;casenull:thrownewArgumentNullException(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 statementstring test ="1";switch(test){case"*":
Console.WriteLine("test");break;case"Something else":
Console.WriteLine("test1");break;casestringwhen 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",stringwhen test !="*"=>"test2",
_ =>"default"// In switch expressions the _ is the same as default};
Console.WriteLine(result);