打开 Nullable Boolean :当值为 true 时 case 变为 null [英] Switch on Nullable Boolean : case goes to null when value is true

查看:20
本文介绍了打开 Nullable Boolean :当值为 true 时 case 变为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到处理可为空类型的正确方法是使用 HasValue 属性.但我想知道为什么下面的 switch 语句会在 null 情况下而不是默认情况下中断.使用 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;
        }
    }

编辑:如果仅指定了case Nulldefault,则任何Nullable 都会观察到行为.>

Edit: Behavior is observed with any Nullable if only case Null and default is specified.

推荐答案

这将是一个非常简短的答案:您只需点击 Roslyn bug #4701,两周前报告.

This is going to be a very short answer: You just hit Roslyn bug #4701, reported two weeks ago.

里程碑设置为 1.1,因此现在您必须使用单独的 if 子句解决此问题,同时等待下一次编译器更新.

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.

这篇关于打开 Nullable Boolean :当值为 true 时 case 变为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆