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
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.