5/7/11

How to Use a Goto Statement in C#

The Visual C# programming language supports the use of the goto keyword. This support allows the developer to declare a label location in the code and use the goto statement to return to it from anywhere else in the program. It is a powerful but simple tool that developers can use to simplify the design of programs when they use it with moderation. Programmers should avoid overusing the goto statement, however, as it can lead to confusing code.
    • 1

      Open Visual C#.

    • 2

      Paste the following into the code editor within the "public static void Main()" method:

      Start:

      Console.WriteLine("Print this line.");

      goto Start;

      This code declares the "Start" location, prints a line to the console and goes back to the start label, resulting in an infinite loop.

    • 3

      Click the green arrow to test your code. You can stop it again by clicking the blue square.

  • No comments: