goto in C#

goto in C#

goto label;
label:

using System;
class Ok
{
public static void Main()
{
Console.WriteLine(“Let’s check ‘goto'”);
goto pak;
Console.WriteLine(“It’s not displayed”);
pak:
Console.WriteLine(“It’s displayed”);
}
}

We have the result:

D:\SharpDevelop Projects\CSharp>csc cs15.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.

cs15.cs(8,1): warning CS0162: Unreachable code detected

D:\SharpDevelop Projects\CSharp>cs15
Let’s check ‘goto’
It’s displayed

D:\SharpDevelop Projects\CSharp>

It’s not good to use goto too often. We should avoid it, but sometimes we need that.

Leave a comment