Just a quick one, but what do you think will be the output of this program? It’s not what I expected:
class Program { static void Main() { CanILeak leakyRef = null; try { new CanILeak(cil => leakyRef = cil); } catch (Exception) { Console.WriteLine("Exception"); } if (leakyRef != null) leakyRef.AreYouStillThere(); Console.ReadLine(); } } class CanILeak { public CanILeak(Action fail) { fail(this); throw new Exception(); } public void AreYouStillThere() { Console.WriteLine("I'm still here"); } }
1 comment
Comments feed for this article
28 June 2010 at 1:07 pm
Jeremy Walker
Interesting one.
If the constructor took a CanILeak, and set the value to itself, then it would surely depend on whether the variable was passed with the ref keyword or not. I have no idea what the scoping is for the lambda expression. If the expression is passed and executed in the context of the new object (in effect, leakyRef has been passed without ref) , I’d say your output would just be “Exception”. If it is executed in the context if Main() then you’d get both lines of output.
I’m going to go for the latter. I’m working in Linux atm, else I’d have a play. Let me know which it was, and why š