开机可空布尔:案件进入空当值为true [英] Switch on Nullable Boolean : case goes to null when value is true
问题描述
我认识来处理可空类型的正确方法是使用HasValue属性。不过,我想知道为什么空的情况下,而不是默认以下switch语句休息。使用VS2015 C#4.0。正在使用VS2010 C#4.0的另一台计算机没有此相同的问题。
I realize the proper way to handle nullable types is to use the HasValue property. But I would like to know why the following switch statement breaks on the null case instead of default. Using VS2015 C#4.0. Another computer that is using VS2010 C#4.0 does not have this same problem.
private void Testing()
{
bool? boolValue = true;
switch (boolValue)
{
case null:
break; //even though value is true, code runs here
default:
break;
}
}
修改:行为与任何可空
如果只情况下,空
和观察到的默认
中指定。
Edit: Behavior is observed with any Nullable
if only case Null
and default
is specified.
推荐答案
这将是一个非常简短的回答:你刚才打的罗斯林BUG#4701 ,报道两个星期前。
This is going to be a very short answer: You just hit Roslyn bug #4701, reported two weeks ago.
里程碑设置为1.1,所以现在你将有一个单独的如果
子句来解决此,而等待下一个编译器的更新。
The milestone is set to 1.1, so right now you'll have to workaround this with a separate if
clause, while waiting for the next compiler update.
这篇关于开机可空布尔:案件进入空当值为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!