Lesson 5 of 15

Switch Expressions

Switch Expressions

Modern C# (8+) has switch expressions — a concise pattern-matching syntax:

int day = 3;
string name = day switch {
    1 => "Monday",
    2 => "Tuesday",
    3 => "Wednesday",
    4 => "Thursday",
    5 => "Friday",
    _ => "Weekend",
};
Console.WriteLine(name); // Wednesday

The _ is the discard pattern — it matches anything not handled above.

You can also use switch expressions with conditions:

int score = 75;
string grade = score switch {
    >= 90 => "A",
    >= 80 => "B",
    >= 70 => "C",
    >= 60 => "D",
    _ => "F",
};

Your Task

Write a static method Season(int month) that returns the season:

  • Months 12, 1, 2 → "Winter"
  • Months 3, 4, 5 → "Spring"
  • Months 6, 7, 8 → "Summer"
  • Months 9, 10, 11 → "Autumn"
  • Anything else → "Unknown"
WasmSharp (.NET) loading...
Loading...
Click "Run" to execute your code.