int value = 0;
switch (value) {
case 0:
// do something
break;
case 1:
// do something else
break;
default :
// something if anything not match
}
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
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;
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;
}
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;
...
}
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;
}
}
}
}
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));
}
// C# program to illustrate
// switch case statement
using System;
public class GFG {
// Main Method
public static void Main(String[] args)
{
int nitem = 5;
switch (nitem) {
case 1:
Console.WriteLine("case 1");
break;
case 5:
Console.WriteLine("case 5");
break;
case 9:
Console.WriteLine("case 9");
break;
default:
Console.WriteLine("No match found");
break;
}
}
}