C Sharp Goto Statement

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# Goto Statement

The C# goto statement is also known jump statement. It is used to transfer control to the other part of the program. It unconditionally jumps to the specified label.

It can be used to transfer control from deeply nested loop or switch case label.

Currently, it is avoided to use goto statement in C# because it makes the program complex.

C# Goto Statement Example

Let's see the simple example of goto statement in C#.

  1. using System;  
  2. public class GotoExample  
  3.     {  
  4.       public static void Main(string[] args)  
  5.       {  
  6.       ineligible:  
  7.           Console.WriteLine("You are not eligible to vote!");  
  8.   
  9.       Console.WriteLine("Enter your age: ");  
  10.       int age = Convert.ToInt32(Console.ReadLine());  
  11.       if (age < 18){  
  12.               goto ineligible;  
  13.       }  
  14.       else  
  15.       {  
  16.               Console.WriteLine("You are eligible to vote!");   
  17.       }  
  18.       }  
  19.    }  

Output:

You are not eligible to vote!
Enter your age:
11
You are not eligible to vote!
Enter your age:
5
You are not eligible to vote!
Enter your age:
26
You are eligible to vote!