Use goto with a switch statement in CSharp

Description

The following code shows how to use goto with a switch statement.

Example


using System; // w  ww. j  av  a2  s . c  om
 
public class MainClass {     
  public static void Main() { 
 
    for(int i=1; i < 5; i++) { 
      switch(i) { 
        case 1: 
          Console.WriteLine("In case 1"); 
          goto case 3; 
        case 2: 
          Console.WriteLine("In case 2"); 
          goto case 1; 
        case 3: 
          Console.WriteLine("In case 3"); 
          goto default; 
        default: 
          Console.WriteLine("In default"); 
          break; 
      } 
      Console.WriteLine(); 
    } 
  } 
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception