动态变量和switch语句不会中断。 [英] Dynamic variable and switch statement do not break.

查看:70
本文介绍了动态变量和switch语句不会中断。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

dynamic status = HttpStatusCode.OK;
switch (status)
{
    case HttpStatusCode.OK:
        Console.WriteLine("This is OK");
        break;
    case HttpStatusCode.NotFound:
        Console.WriteLine("This is NOTFOUND");
        break;
    default:
        Console.WriteLine("This is default");
        break;
}

我得到了

This is OK
This is default

break;总是违约。这是休息时正确的开关行为吗? ?

break; always comes to default. Is this the correct switch behaviour for break; ?

-.- yoz ...

-.- yoz...

推荐答案

不,这不是正确的行为。出了点问题。

No that's not the correct behavior. Something is wrong.

事实上,我不知道你的代码是如何编译的。状态定义为动态,我会预期会出现如下错误。

In fact, I don't know how your code compiled. With status defined as dynamic, I would have expected an error like the one below.

严重程度    代码    描述    项目    文件    线    抑制状态

错误     CS0151    开关表达式或案例标签必须是bool,char,string,integral,enum或相应的可空类型  


Severity    Code    Description    Project    File    Line    Suppression State
Error    CS0151    A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type  


这篇关于动态变量和switch语句不会中断。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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