-
I was playing with C# 8.0 in Visual Studio 2019 Preview and when I was testing switch expression I wrote something similar to this int? test = 0;
bool? tested = test switch
{
0 => false,
1 => true,
null => null,
_ => null
}; This results in error It's easy to fix by casting any of result values to bool?, however I would like to know if there is any reason why this works in this way? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
expressions in C# are almost never target typed, so declaring tested as a There are various proposals to improve this behaviour for the conditional operator. The same proposals could probably be applied here. |
Beta Was this translation helpful? Give feedback.
-
#450. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@DavidArno Not sure that #2389 already covered nullable. It seem only mention the base type |
Beta Was this translation helpful? Give feedback.
-
#2389 covers the target type, which is the exact case of the OP. I wish it could cover nullable without target-typing (e.g. |
Beta Was this translation helpful? Give feedback.
-
I'd assume #2389 should cover |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h, so you were right. #2389 wouldn't work for |
Beta Was this translation helpful? Give feedback.
-
This would be fixed by either #2389 or #33. It is likely that one or both of these will be included in C# 8. |
Beta Was this translation helpful? Give feedback.
This would be fixed by either #2389 or #33. It is likely that one or both of these will be included in C# 8.