Search code examples
c#goto

Jump to certain line in code C#


I have a few methods that are called from within a few other methods. When in some of the methods a certain action is performed, I would like to go back to the very first method and skip the rest of the code. At the moment, I use booleans to check the "status" of the program but I would like to avoid this because the methods should be void since in essence they don't need to return anything. I found stuff like goto but that only works in the same method.

Question: is there a way to jump to a specific point in the code in a different method in C#? I found stuff on other languages but not a lot on C#.

Current situation:

    void test1()
    {
        bool status = test2();

        if (!status)
            return; // the other stuff will not get done

        Debug.WriteLine("Initialization OK");
    }

    bool test2()
    {
        bool status = test3();

        if (!status)
            return false; // the other stuff will not get done

        // do other stuff 
        return true;
    }

    bool test3()
    {
        if (xxx)
            return false; // the other stuff will not get done
        else
            // do other stuff 
            return true;
    }

Wanted situation:

    void test1()
    {
        test2();

        // do other stuff
        Debug.WriteLine("Initialization OK");

        GOTOHERE:
             Debug.WriteLine("Initialization NOT OK");
    }

    void test2()
    {
        test3();            
        // do other stuff 
    }

    void test3()
    {
        if (xxx)
            **GOTOHERE**; // Go directly to the location in test1() so that all unnecessary code is skipped

        // do other stuff
    }

Solution

  • Returning something from your method to indicate what you should do afterwards is exactly what you should do. Thus return a boolean indicating if test2 or test3 succeeded and use that value to indicate if you want to proceed further. Don´t use goto nowadays as it only leeds to spaghetti-code, that is hard to maintain. To determine under which circumstances control-flow should jump to GOTOHERE you´d need to scan your entire code for that specific goto-statement.

    In your case you want to indicate if some initialization-code works correct. Thus you can also throw an exception:

    void test3()
    {
        if (xxx)
            throw new Exception("Some text");
    
        // do other stuff
    }
    

    This way you don´t need to return anything from your method, but handle the exception appropriately:

    void test1()
    {
        try { test2(); }
        catch { 
            // some exception-handling such as logging
            return; 
        }
    
        Debug.WriteLine("Initialization OK");
    }
    

    This has the advantage that you don´t need to check in test2 if test3 succeeded, allowing you to let the exception bubble through your methods until it is finally handled by a catch. If no catch was found in the entire callstack your app will probably terminate.