Vishful thinking…

‘goto’ statement in C# ??? !!!

Posted in C# by viswaug on August 24, 2007

I am actually really surprised to find out that there is a ‘goto’ statement in C#. This really does come as a surprise to me after years of being preached the evilness of the ‘goto’ statements. Maybe it is not all that bad. But I have not needed to use it before and hopefully never will.

4 Responses

Subscribe to comments with RSS.

  1. Spacix said, on September 6, 2007 at 7:21 pm

    You sort of need it because you can’t fall though case statement like in C++
    the only way to get this

    int i = 3;
    switch(i)
    {
    case 1:
    i+=2;
    case 3:
    printf(“%d”,i);
    break;
    }

    in C# (without having it look non-similar) is with a “goto” statement:

    int i = 3;
    switch(i)
    {
    case 1:
    i+=2;
    goto case 3;
    case 3:
    printf(“%d”,i);
    break;
    }

  2. Simon said, on February 28, 2008 at 10:08 pm

    The other reason you need it is to break out of nested loops. Imagine writing code to search a 2-D array for an instance of a zero entry:


    int i = 0, j = 0;
    for (; i < rows; ++i)
    for (; j < cols; ++j)
    if (array[i,j] == 0)
    goto FoundZero;

    FoundZero:
    // Do something else...

    It’s possible to rewrite this to avoid the goto, but very hard to do this while maintaining the same efficiency or simplicity of the above code.

    It’s also possible to embed the search in a separate function and use a return statement to break out of the loop, but again I think this detracts from the legibility of what is a simple piece of code.

    Personally, I greatly prefer the labeled break that Java has, which provides the same loop breaking functionality as this, but avoids any potential for misuse. In fact the goto method forces you to have a statement after the loop that you jump to, which is occasionally annoying, plus aesthetically it feels better to say “break of of this loop” rather than “jump to this unrelated statement”.

    As for the switch usage, I wish C#’s designers had chosen to implement their own more sensible control statement, rather than emulating the warts and quirks of C.

    As a final comment, I will say that it often amuses me how fearful are about goto, as if using it will suddenly turn all their code into spaghetti and send them over to the dark side. I think this paranoia comes from a generation of programmers who were raised on BASIC as kids, developed bad habits there, and then had those habits beaten out of them as they became more mature programmers. Really, goto is just fine as long as you use it for short range local hops in clearly structured code. If it leads to clearer, more easily understood code than any other technique, then by definition it is the Right Thing To Do.

  3. Jakob said, on September 7, 2009 at 7:50 am

    I never understood why everyone preaches against goto. I like it. It is a simple, easy to understand keyword, that has many uses.

    Apart from breaking out of deeply nested loops, and doing voluntary fall through in switch statements, i have used it a lot in protocol drivers to control retries and resends.

  4. Forrest said, on January 19, 2010 at 8:01 pm

    I haven’t used a GOTO statement in code in more than a decade. They’re really not as evil as people make them out to be, I think, but once you get into the habit of structuring your code in ways that don’t benefit from them … you sort of forget that option is there.

    In the ancient days, long before my time, I can see how they were indispensable instead of indefensible. I can’t think of another good way to re-use code in circa 1970 BASIC, for example, and that drives home the point about Turing completeness…


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

%d bloggers like this: