C#开关/中断 [英] C# switch/break

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

问题描述

似乎我需要使用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?

非常感谢,非常感谢!

推荐答案

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

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;
}

这篇关于C#开关/中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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