C# - Swtich case fall through

It is a very common practice on C to have switch case label fall through, however, it is nowadays considered as a very dangerous thing to do in modern programming languages, C# is one of the language that does not allow fall through.
However, C# does provide something that can support label jumping with switch case statement. I will show you an example on how to use the label jumping to support fall through.
the code as below.

class Program
  {
    /** this will show you one how to fall through from one case branch to another case branch */
    static void Main(string[] args)
    {

      for (int i = 0; i < 5; i++)
      {
        var step = i;
        Console.WriteLine("=====================");
        Console.WriteLine("Step = {0}", step);

        switch (step)
        {
          case 0:
            Console.WriteLine(0);
            break;
          case 1:
            Console.WriteLine(1);
            goto case 0;
          case 2:
            Console.WriteLine(2);
            goto case 1;
          case 3:
            Console.WriteLine(3);
            goto case 2;
          default:
            Console.WriteLine("...");
            break;
        }
      }
    }
  }

转载于:https://my.oschina.net/u/854138/blog/101291

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值