switch expression c# examples
Example 1: syntaxe switch c#
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 2: C# Switch
int var = 3;
switch (var)
{
case 1:
Console.WriteLine("Ahoy!");
break;
case 2:
Console.WriteLine("Hi!");
break;
case 3:
Console.WriteLine("HELLO!");
break;
}
//Output: HELLO!