C# 开关/中断

发布于 2024-08-12 12:02:29 字数 133 浏览 7 评论 0原文

看来我需要使用 C# 在 switch 语句中的每个 case 块中使用中断。

我可以在其他语言中看到这样做的原因,您可以在其中转到下一个案例陈述。

案例块是否有可能落入其他案例块?

非常感谢,非常感谢!

It appears I need to use a break in each case block in my switch statement using C#.

I can see the reason for this in other languages where you can fall through to the next case statement.

Is it possible for case blocks to fall through to other case blocks?

Thanks very much, really appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩世 2024-08-19 12:02:29

是的,您可以通过两种方式进入下一个案例块。您可以使用不需要中断的空案例,也可以使用 goto 跳转到下一个(或任何)案例:

switch (n) {
  case 1:
  case 2:
  case 3:
    Console.WriteLine("1, 2 or 3");
    goto case 4;
  case 4:
    Console.WriteLine(4);
    break;
}

Yes, you can fall through to the next case block in two ways. You can use empty cases, which don't need a break, or you can use goto to jump to the next (or any) case:

switch (n) {
  case 1:
  case 2:
  case 3:
    Console.WriteLine("1, 2 or 3");
    goto case 4;
  case 4:
    Console.WriteLine(4);
    break;
}
诺曦 2024-08-19 12:02:29

执行“break”是为了阻止错误。如果您需要强制失败,请使用“goto case”(将其替换为适当的值),

以下示例显示了您可以执行的操作:

switch(n)
{
    case 1:
    case 2:
      //do something for 1+2
      //...
      goto case 3;
    case 3:
      //do something for 3, and also extra for 1+2
      //...
      break;
    default:
      //do something for all other values
      //...
      break;
}

请参阅 http://msdn.microsoft.com/en-us/library/06tc147t%28VS.80%29.aspx

The enforcement of "break" is there to stop bugs. If you need to force a fall-thru then use "goto case " (replace the with appropriate value)

the following example shows what you can do:

switch(n)
{
    case 1:
    case 2:
      //do something for 1+2
      //...
      goto case 3;
    case 3:
      //do something for 3, and also extra for 1+2
      //...
      break;
    default:
      //do something for all other values
      //...
      break;
}

See http://msdn.microsoft.com/en-us/library/06tc147t%28VS.80%29.aspx

临风闻羌笛 2024-08-19 12:02:29

C# 不支持隐式失败构造,但 break(或 goto)仍然必须存在(msdn)。您唯一能做的就是按以下方式堆栈案例:

switch(something) {
    case 1:
    case 2:
      //do something
      break;
    case 3:
      //do something else
}

但是 break (或另一个跳转语句,如 goto)只需要在那里。

C# doesn't support implicit fall through construct, but the break (or goto) nonetheless has to be there (msdn). The only thing you can do is stack cases in the following manner:

switch(something) {
    case 1:
    case 2:
      //do something
      break;
    case 3:
      //do something else
}

but that break (or another jump statement like goto) just needs to be there.

初心 2024-08-19 12:02:29

在我的 C#(.NET 1.1、CF)代码中,这两种情况都是允许的:

switch (_printerChoice) 
{
    case BeltPrintersEnum.ZebraQL220: 
        return new ZebraQL220Printer();
        break;
    case BeltPrintersEnum.ONeal: 
        return new ONealPrinter();
        break;
    default:            
        return new ZebraQL220Printer();         
                        break;  
}

switch (_printerChoice) 
{
    case BeltPrintersEnum.ZebraQL220: 
        return new ZebraQL220Printer();
    case BeltPrintersEnum.ONeal: 
        return new ONealPrinter();
    default:            
        return new ZebraQL220Printer();         
}

...但是随着中断的出现,它们会变灰,因此被认为没有实际意义。因此,至少就我而言,它们是允许的,但不是必需的。

In my C# (.NET 1.1, CF) code, both of these are allowed:

switch (_printerChoice) 
{
    case BeltPrintersEnum.ZebraQL220: 
        return new ZebraQL220Printer();
        break;
    case BeltPrintersEnum.ONeal: 
        return new ONealPrinter();
        break;
    default:            
        return new ZebraQL220Printer();         
                        break;  
}

switch (_printerChoice) 
{
    case BeltPrintersEnum.ZebraQL220: 
        return new ZebraQL220Printer();
    case BeltPrintersEnum.ONeal: 
        return new ONealPrinter();
    default:            
        return new ZebraQL220Printer();         
}

...but with the breaks in, they are grayed out, so considered moot. So, at least in my case, they are allowed but not required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文