[Solved] How does a function with multiple return statements work?


The return statement exits the method and returns the given value if the return type is other than void.
The only statements in the method that are executed after a return statement are the ones in a finally block or the disposal of objects of a using-block (which essentially is a special form of try-finally):

private void TestMethod()
{
    // Do something
    if (conditionIsMet)
        return; // Exits the method immediately

    try
    {
        // Do something
        if (conditionIsMet)
            return;  // Statements in finally block will be executed before exiting the method
    }
    finally
    {
        // Do some cleanup
    }

    using (var disposableObj = new DisposableObject())
    {
        // Do something
        if (conditionIsMet)
            return;  // disposableObj will be disposed before exiting the method
    }
}

2

solved How does a function with multiple return statements work?