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

你可能感兴趣的:(C#)