The notorious goto statement, enables you to jump to a labeled statement.
using System; goto lable1; Console.WriteLine("This won't be executed."); lable1: Console.WriteLine("This will be executed."); Console.WriteLine("Hello World!");
You will get compile warning that:
Program.cs(5,1): warning CS0162: Unreachable code detected
Ignore the warning and run the program, you will get the output:
This will be executed. Hello World!
- Cannot jump out of a class.
- Cannot jump into a block of code since
labelhave to be in the same scope.
- Cannot exit a
try ... catchblock.
I believe that few developers like to use goto statement in their projects.