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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s