Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
C# Keywords
 goto
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
goto (C# Reference)

The goto statement transfers the program control directly to a labeled statement.

A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement.

The goto statement is also useful to get out of deeply nested loops.

The following example demonstrates using goto in a switch statement.

C#
class SwitchTest
{
    static void Main()
    {
        Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
        Console.Write("Please enter your selection: ");
        string s = Console.ReadLine();
        int n = int.Parse(s);
        int cost = 0;
        switch (n)
        {
            case 1:
                cost += 25;
                break;
            case 2:
                cost += 25;
                goto case 1;
            case 3:
                cost += 50;
                goto case 1;
            default:
                Console.WriteLine("Invalid selection.");
                break;
        }
        if (cost != 0)
        {
            Console.WriteLine("Please insert {0} cents.", cost);
        }
        Console.WriteLine("Thank you for your business.");

        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/*
Sample Input:  2

Sample Output:
Coffee sizes: 1=Small 2=Medium 3=Large
Please enter your selection: 2
Please insert 50 cents.
Thank you for your business.
*/

The following example demonstrates using goto to break out from nested loops.

C#
public class GotoTest1
{
    static void Main()
    {
        int x = 200, y = 4;
        int count = 0;
        string[,] array = new string[x, y];

        // Initialize the array:
        for (int i = 0; i < x; i++)

            for (int j = 0; j < y; j++)
                array[i, j] = (++count).ToString();

        // Read input:
        Console.Write("Enter the number to search for: ");

        // Input a string:
        string myNumber = Console.ReadLine();

        // Search:
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                if (array[i, j].Equals(myNumber))
                {
                    goto Found;
                }
            }
        }

        Console.WriteLine("The number {0} was not found.", myNumber);
        goto Finish;

    Found:
        Console.WriteLine("The number {0} is found.", myNumber);

    Finish:
        Console.WriteLine("End of search.");


        // Keep the console open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/*
Sample Input: 44

Sample Output
Enter the number to search for: 44
The number 44 is found.
End of search.
*/

For more information, see the following sections in the C# Language Specification:

  • 5.3.3.10 Break, continue, and goto statements

  • 8.9.3 The goto statement

Community Content   What is Community Content?
Add new content RSS  Annotations
goto      atirado ... Thomas Lee   |   Edit   |   Show History
Any programmer using this should be shot dead, then cloned and shot again ad infinitum
Re: goto      Limited Atonement   |   Edit   |   Show History

I disagree. Perhaps I haven't tried to debug enough code with gotos in it, which I've heard is often frustrating beyond reason, but I haven't found myself agreeing to the blind slashing of goto practices. It's left in the language, and included in nearly EVERY programming language on earth for GOOD reasons!! Here and there (I think once so far) I've found a place where GOTO is the clear answer. For instance, when parsing, and checking for compliance from a string, I might use goto to specify that the string is not compliant. What are the alternatives? One might be to create a while(true){} loop, and use break; when a non-compliant situation arises (and put break; at the end). This might be endorsed by some who spit at ALL goto users without asking questions. This is not only not as clear as GOTO, but less clear, because you look at while(true) and think "Why would the fella want an infinite loop? Obviously it's not infinite, now I get to dig through and figure out what this is...oh, it's not a loop at all!! Just a goto-hack as though we don't have 'goto.'" Another response might be to `return false;' which, again, is not as good as `goto NOT_COMPLIANT'; because what's happenning is not as obvious. Also, having nested if statements ad nosium is nosiating:

bool _success = false;
if (compliant_at_char1)
{
if (compliant_at_char2)
{
if (compliant_at_char3)
{
_success = true;
}
}
}
if (_success) ...


This might be more clear than the other examples, but tedius to write, difficult to expand/improve/maintain, and hard on the eyes. There are more alternatives, but I haven't found that goto is always replaceable with the same effect.
You don't like goto, fine, don't use it, but I think it isn't advisable to advise execution (multiple times) to those who use the statement which is, again, a language feature, not only here as an overlooked feature that should have been weeded out, but one added to nearly every language today.

-Aaron

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker