What really happens in a try { return x; } finally { x = null; } statement?

The finally statement is executed, but the return value isn’t affected. The execution order is:

  1. Code before return statement is executed
  2. Expression in return statement is evaluated
  3. finally block is executed
  4. Result evaluated in step 2 is returned

Here’s a short program to demonstrate:

using System;

class Test
{
    static string x;

    static void Main()
    {
        Console.WriteLine(Method());
        Console.WriteLine(x);
    }

    static string Method()
    {
        try
        {
            x = "try";
            return x;
        }
        finally
        {
            x = "finally";
        }
    }
}

This prints “try” (because that’s what’s returned) and then “finally” because that’s the new value of x.

Of course, if we’re returning a reference to a mutable object (e.g. a StringBuilder) then any changes made to the object in the finally block will be visible on return – this hasn’t affected the return value itself (which is just a reference).

Leave a Comment