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")
};