switch case or condition c# code example
Example 1: c# switct case
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;
}
}
}
Example 2: c# switch
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;
}
}
}
Example 3: c# switch case with or condition
int i = 5;
switch (i)
{
case(1):
case(2):
Console.WriteLine(i);
break;
default:
break;
}
Example 4: c# switch case
public class Example
{
public void Click(object sender, RoutedEventArgs e)
{
if (sender is Button handler)
{
switch (handler.Tag.ToString())
{
case string tag when tag.StartsWith("Example"):
break;
default:
break;
}
}
}
}