I saw this tip in another question and was wondering if someone could explain to me how on earth this works?
try { return x; } finally { x = null; }
I mean, does the finally clause really execute after the return statement? How thread-unsafe is this code? Can you think of any additional hackery that can be done w.r.t. this try-finally hack?
No – at the IL level you can’t return from inside an exception-handled block. It essentially stores it in a variable and returns afterwards
i.e. similar to:
for example (using reflector):
compiles to:
This basically declares a local variable (
CS$1$0000), places the value into the variable (inside the handled block), then after exiting the block loads the variable, then returns it. Reflector renders this as: