Combine return and switch in C#

Actually this is possible using switch expression starting with C# 8.

return a switch
    1 => "lalala",
    2 => "blalbla",
    3 => "lolollo",
    _ => "default"

Switch Expression
There are several syntax improvements here:

  • The variable comes before the switch keyword. The different order
    makes it visually easy to distinguish the switch expression from the
    switch statement.
  • The case and : elements are replaced with =>. It’s
    more concise and intuitive.
  • The default case is replaced with a _
  • The bodies are expressions, not statements.

For more information and examples check:

  • Microsoft’s C# 8 Whats New, and
  • C# Switch Expression.

Leave a Comment