Is empty case of switch in C# combined with the next non-empty one?(C# 中 switch 的空情况是否与下一个非空情况相结合?)
问题描述
使用以下代码:
case "GETSITES":
case "SITESETUP":
MessageBox.Show("Help! I am suffering from the Air-Conditioned Nightmare!!!");
// ...
MessageBox.Show 是否会被执行,开关值是 "GETSITES" 还是 "SITESETUP"?
Will the MessageBox.Show be executed either the switch value is "GETSITES" or "SITESETUP"?
或仅如果开关值为SITESETUP"?
由于 "GETSITES" 没有中断,我想是的,但不确定.
Since "GETSITES" has no break, I'm thinking yes, but am not sure.
更新
我想我应该这样表述我的问题:
I guess the way I should have worded my question as:
这两个代码片段在语义上是等价的吗?
Are these two code fragments semantically equivalent?
片段 1
fragment 1
case 0:
case 1:
// bla bla bla;
break;
片段2(伪代码)
fragment 2(pseudo code)
case 0, 1:
// bla bla bla;
break;
推荐答案
您所描述的是同一案例有两个标签.C# 确实允许这样做.但是,您不能以 C 允许的相同方式进入下一个 case 语句,也就是说,您的标签不能有一些代码,然后进入下一个 case 语句.这是禁止的,会导致编译错误.
What you are describing is having two labels for the same case. C# does allow this. You cannot, however, fall through to the next case statement in the same way that C allows, that is, your label cannot have some code, then fall through to the next case statement. That is forbidden and will cause a compilation error.
这篇关于C# 中 switch 的空情况是否与下一个非空情况相结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 中 switch 的空情况是否与下一个非空情况相结合?
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
