Tsallaka zuwa abun ciki
Justice Tech School

C# Switch

switch matches one value against cases. Modern C# has switch expressions with => and pattern matching, which are shorter and return a value.

Example
int day = 3;
string name = day switch { 1 => "Mon", 2 => "Tue", 3 => "Wed", _ => "Other" };
Console.WriteLine(name);
switch (day) { case 6: case 7: Console.WriteLine("Weekend"); break; default: Console.WriteLine("Weekday"); break; }

Exercise

The fallback case.

switch (x) { : break; }

Quiz Test

What does _ mean in a switch expression?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.