C#中的switch语句失败? [英] Switch statement fallthrough in C#?

查看:18
本文介绍了C#中的switch语句失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Switch 语句失败是我个人喜欢 switchif/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-cases 可以通过在 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 ifs?

解决方案

(Copy/paste of an answer I provided elsewhere)

Falling through switch-cases 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语句失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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