9

Visual Studio 2019 recommended converting a switch statement I had written to a switch expression (both included below for context).

For a simple example such as this, is there any technical or performance advantage to writing it as an expression? Do the two versions compile differently for example?

Statement

switch(reason)
{
    case Reasons.Case1: return "string1";
    case Reasons.Case2: return "string2";
    default: throw new ArgumentException("Invalid argument");
}

Expression

return reason switch {
    Reasons.Case1 => "string1",
    Reasons.Case2 => "string2",
    _ => throw new ArgumentException("Invalid argument")
};
4
  • 2
    In your example I would say the only advantage is that the latter is less verbose and thereby more readable (once you get used to the new syntax). For extended usage one big advantage is that you can return a value directly from the switch - less procedural code.
    – DaggeJ
    Apr 16, 2020 at 9:54
  • 2
    It's more readable, I'd like to say Apr 16, 2020 at 9:55
  • How does your research re technical switch statement vs expression pros & con apply? Because otherwise you are asking for SO content to be duplicated & you show no research.
    – philipxy
    Apr 16, 2020 at 23:59
  • 2
    While case Reasons.Case1 is redundant, personally I much prefer default: written out than to have to intuit that _ => means default... so it's a tradeoff in terms of readability to me, with the statement actually edging a bit ahead of the expression.
    – TylerH
    Apr 17, 2020 at 14:07

1 Answer 1

11

In the example you give there's not a lot in it really. However, switch expressions are useful for declaring and initializing variables in one step. For example:

var description = reason switch 
{
    Reasons.Case1 => "string1",
    Reasons.Case2 => "string2",
    _ => throw new ArgumentException("Invalid argument")
};

Here we can declare and initialize description immediately. If we used a switch statement we'd have to say something like this:

string description = null;
switch(reason)
{
    case Reasons.Case1: description = "string1";
                        break;
    case Reasons.Case2: description = "string2";
                        break;
    default:            throw new ArgumentException("Invalid argument");
}

One downside of switch expressions at the moment (in VS2019 at least) is that you can't set a breakpoint on an individual condition, only the whole expression. However, with switch statements you can set a breakpoint on an individual case statement.

3
  • While it does look better than the usual switch, are there any benefits? Like, let's say, you call it a million times, will it be faster?
    – Sheradil
    Apr 16, 2020 at 9:59
  • 4
    @Sheradil Chances are both versions will produce essentially identical IL anyway so I doubt there's a perf benefit.
    – DavidG
    Apr 16, 2020 at 10:10
  • 1
    The page here shows a benchmark indicating that IfElse is slightly faster than Statement, and Expression is noticeably slower than both. I have not run this myself.
    – Mmm
    Jul 28, 2022 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.