Switch statement fallthrough in C#?(C#中的switch语句失败?)
问题描述
Switch 语句失败是我个人喜欢 switch
与 if/else if
构造的主要原因之一.这里有一个例子:
静态字符串 NumberToWords(int number){字符串 [] 数字 = 新字符串 []{ , 一二三四五",六"、七"、八"、九"};字符串[] 十位 = 新字符串[]{",",二十",三十",四十",五十",六十"、七十"、八十"、九十"};字符串 [] 青少年 = 新字符串 []{ 十",十一",十二",十三",十四",十五",十六"、十七"、十八"、十九"};字符串答案 = "";开关 (number.ToString().Length){案例3:ans += string.Format("{0} 百和", numbers[number/100]);案例2:int t = (数字/10) % 10;如果(t == 1){ans += teens[数量 % 10];休息;}否则如果 (t > 1)ans += string.Format("{0}-", tens[t]);情况1:int o = 数字 % 10;ans += 数字[o];休息;默认:抛出新的 ArgumentException("number");}返回答案;}
聪明的人之所以畏缩,是因为
string[]
应该在函数之外声明:嗯,他们是,这只是一个例子.编译器失败并出现以下错误:
<上一页>控制不能从一个案例标签(案例 3:")转移到另一个案例标签控制不能从一个案例标签(案例 2:")转移到另一个案例标签
为什么?有没有办法在没有三个 if
的情况下获得这种行为?
(复制/粘贴 我在别处提供的答案)
通过 switch
-case
s 可以通过在 case
中没有代码来实现(参见 case 0
),或使用特殊的goto case
(参见case 1
)或goto default
(参见case 2
)表格:
switch (/*...*/) {case 0://与 case 1 共享完全相同的代码情况1://做一点事转到案例 2;案例2://做其他事情转到默认值;默认://做一些完全不同的事情休息;}
Switch statement fallthrough is one of my personal major reasons for loving switch
vs. if/else if
constructs. An example is in order here:
static string NumberToWords(int number)
{
string[] numbers = new string[]
{ "", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
string[] tens = new string[]
{ "", "", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety" };
string[] teens = new string[]
{ "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen" };
string ans = "";
switch (number.ToString().Length)
{
case 3:
ans += string.Format("{0} hundred and ", numbers[number / 100]);
case 2:
int t = (number / 10) % 10;
if (t == 1)
{
ans += teens[number % 10];
break;
}
else if (t > 1)
ans += string.Format("{0}-", tens[t]);
case 1:
int o = number % 10;
ans += numbers[o];
break;
default:
throw new ArgumentException("number");
}
return ans;
}
The smart people are cringing because the string[]
s should be declared outside the function: well, they are, this is just an example.
The compiler fails with the following error:
Control cannot fall through from one case label ('case 3:') to another Control cannot fall through from one case label ('case 2:') to another
Why? And is there any way to get this sort of behaviour without having three if
s?
(Copy/paste of an answer I provided elsewhere)
Falling through switch
-case
s can be achieved by having no code in a case
(see case 0
), or using the special goto case
(see case 1
) or goto default
(see case 2
) forms:
switch (/*...*/) {
case 0: // shares the exact same code as case 1
case 1:
// do something
goto case 2;
case 2:
// do something else
goto default;
default:
// do something entirely different
break;
}
这篇关于C#中的switch语句失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C#中的switch语句失败?


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01