Home > Articles

Introducing C#

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Goto

Just as in the golden age, Microsoft provides the goto command. Many of you know this feature from ancient versions of the BASIC programming language. C# provides jumps as well. With the help of goto, it's possible to jump to any position inside the program and to continue working there. To find out how this works, we've implemented a trivial example:

using System;

class  Hello
{
    public static void Main()
    {
        Console.WriteLine("Paul is married");
        goto nobodyknows;
        Console.WriteLine("Paul plays around with other women");

nobodyknows:
        Console.WriteLine("Paul is in love");
    }
}

After processing the first line, the program jumps to nobodyknows. Luckily, those evil things aren't displayed and the output isn't too dangerous for Paul:

[hs@duron mono]$ mono ex.exe
Paul is married
Paul is in love

Try to avoid jumps when possible; otherwise, your programs will be complex and painful to maintain. It's much better to use functions, but we'll deal with modules later in this book.

  • + Share This
  • 🔖 Save To Your Account