Switch Expression Block #7781
-
Can you use blocks in switch expressions? var ret = input switch {
_ => {
return default;
},
}; I feel like this should work but it doesnt seem to. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
@TonyValenti It was suggested before, in fact I suggested it as well back then when they designed it and the response by Neal Gafter was to use local functions. |
Beta Was this translation helpful? Give feedback.
-
Duplicate of #3037. |
Beta Was this translation helpful? Give feedback.
-
Yes please. Currently, I have to resort to solutions like: public static T Do<T>(Func<T> f) => f();
int x = ....;
int y = x switch {
< 3 => 42,
> 10 => 69,
_ => Do<int>(() =>
{
// logic goes here
return -1;
}),
} or even worse (readability-wise): int x = ....;
int y = x switch {
< 3 => 42,
> 10 => 69,
_ => new Func<int>(() =>
{
// logic goes here
return -1;
})(),
} |
Beta Was this translation helpful? Give feedback.
@TonyValenti It was suggested before, in fact I suggested it as well back then when they designed it and the response by Neal Gafter was to use local functions.