c# 8 switch 表达式不够 "smart"
c# 8 switch expression not "smart" enough
这段代码很简单,只是一个普通的开关:
bool? isSomething = strSomething switch
{
"I" => true,
"D" => false,
_ => null,
};
但是,编译器给我以下错误:
CS0037 Cannot convert null to 'bool' because it is a non-nullable value type
这个变量显然是一个可以为空的 bool bool?
,为什么 c# 编译器不能解决这个问题而我必须强制转换为 null 才能让它工作:
_ => (bool?)null,
我没听懂吗?演员不是不必要的吗?
有一个已打开的 issue #2387 for this in c# lang. Which could be fixed in this candidate for c# 9。
这段代码很简单,只是一个普通的开关:
bool? isSomething = strSomething switch
{
"I" => true,
"D" => false,
_ => null,
};
但是,编译器给我以下错误:
CS0037 Cannot convert null to 'bool' because it is a non-nullable value type
这个变量显然是一个可以为空的 bool bool?
,为什么 c# 编译器不能解决这个问题而我必须强制转换为 null 才能让它工作:
_ => (bool?)null,
我没听懂吗?演员不是不必要的吗?
有一个已打开的 issue #2387 for this in c# lang. Which could be fixed in this candidate for c# 9。