这段代码很简单,只是一个普通的开关:
bool? isSomething = strSomething switch
{
"I" => true,
"D" => false,
_ => null,
};
但是,编译器给出了以下错误:
CS0037 Cannot convert null to 'bool' because it is a non-nullable value type
这个变量显然是一个可为 null 的 bool bool?
,为什么 c# 编译器不能在我不必强制转换 null 来使其工作的情况下弄清楚这一点:
_ => (bool?)null,
我没听懂吗? Actor 阵容不是不必要的吗?
最佳答案
有一个打开的issue #2387为此,在 c# lang 中。可以在 this candidate 中修复对于 c# 9。
关于switch-statement - C# 8 switch 表达式 "smart"不够,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846038/