Multiple statements in a switch expression: C# 8 [duplicate]

Your only supported choice is the func like you did. See [this article][1] for more information. His example:

var result = operation switch
{
"+" => ((Func<int>)(() => {
    Log("addition");
    return a + b;
}))(),
"-" => ((Func<int>)(() => {
    Log("subtraction");
    return a - b;
}))(),
"/" => ((Func<int>)(() => {
    Log("division");
    return a / b;
}))(),
_ => throw new NotSupportedException()
};

Just because switch expressions are new doesn’t mean they are the best for all use cases. They are not designed to contain multiple commands.

Edit: I suppose you could also simply call external functions instead of making anonymous ones.
[1]: https://alexatnet.com/cs8-switch-statement/

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)