真的发生了什么{try {return x; } finally {x = null; }声明?

我在另外一个问题上看到了这个提示,并想知道是否有人能够向我解释这个工作如何?

try { return x; } finally { x = null; } 

我的意思是, finally语句是否真的 return语句之后执行? 这个代码如何线程不安全? 你能想到任何额外的hackery可以做到这一点try-finally破解?

否 – 在IL级别,不能从exception处理块内返回。 它基本上存储在一个variables,然后返回

即类似于:

 int tmp; try { tmp = ... } finally { ... } return tmp; 

例如(使用reflection器):

 static int Test() { try { return SomeNumber(); } finally { Foo(); } } 

编译为:

 .method private hidebysig static int32 Test() cil managed { .maxstack 1 .locals init ( [0] int32 CS$1$0000) L_0000: call int32 Program::SomeNumber() L_0005: stloc.0 L_0006: leave.s L_000e L_0008: call void Program::Foo() L_000d: endfinally L_000e: ldloc.0 L_000f: ret .try L_0000 to L_0008 finally handler L_0008 to L_000e } 

这基本上声明了一个局部variables( CS$1$0000 ),将值放入variables(在处理块内),然后退出块加载variables,然后返回它。 reflection器呈现为:

 private static int Test() { int CS$1$0000; try { CS$1$0000 = SomeNumber(); } finally { Foo(); } return CS$1$0000; } 

finally语句被执行,但返回值不受影响。 执行顺序是:

  1. 执行return语句之前的代码
  2. 评估返回语句中的expression式
  3. 最后块被执行
  4. 返回步骤2中评估的结果

这里有一个简短的程序来演示:

 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"; } } } 

这将打印“尝试”(因为这是什么返回),然后“最后”,因为这是新的价值。

当然,如果我们返回一个可变对象的引用(例如一个StringBuilder),那么在finally块中对对象所做的任何更改都将在返回时可见 – 这不会影响返回值本身(这只是一个参考)。

finally子句在返回语句之后但在从函数实际返回之前执行。 这与线程安全无关,我想。 这不是破解 – 无论你在try块或catch块中做什么,finally都会保证总是运行。

加上Marc Gravell和Jon Skeet给出的答案,重要的是要注意对象和其他引用types在返回时行为类似,但确实有一些差异。

返回的“什么”遵循与简单types相同的逻辑:

 class Test { public static Exception AnException() { Exception ex = new Exception("Me"); try { return ex; } finally { // Reference unchanged, Local variable changed ex = new Exception("Not Me"); } } } 

在本地variables在finally块中被分配一个新的引用之前,已经评估了正在返回的引用。

执行本质上是:

 class Test { public static Exception AnException() { Exception ex = new Exception("Me"); Exception CS$1$0000 = null; try { CS$1$0000 = ex; } finally { // Reference unchanged, Local variable changed ex = new Exception("Not Me"); } return CS$1$0000; } } 

不同之处在于,如果不小心,可以使用对象的属性/方法来修改可变types,这可能会导致意外的行为。

 class Test2 { public static System.IO.MemoryStream BadStream(byte[] buffer) { System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer); try { return ms; } finally { // Reference unchanged, Referenced Object changed ms.Dispose(); } } } 

关于try-return-finally需要考虑的第二件事情是,返回后仍然可以修改通过引用传递的参数。 只有返回值已被评估并存储在一个等待返回的临时variables中,其他任何variables仍按正常方式修改。 out参数的合约甚至可以不执行,直到最终阻止这种方式。

 class ByRefTests { public static int One(out int i) { try { i = 1; return i; } finally { // Return value unchanged, Store new value referenced variable i = 1000; } } public static int Two(ref int i) { try { i = 2; return i; } finally { // Return value unchanged, Store new value referenced variable i = 2000; } } public static int Three(out int i) { try { return 3; } finally { // This is not a compile error! // Return value unchanged, Store new value referenced variable i = 3000; } } } 

像其他任何stream程构造一样,“try-return-finally”也有它自己的位置,并且可以比编写它实际编译的结构允许更清晰的代码。 但必须谨慎使用,以避免陷入困境。

如果x是一个局部variables,我没有看到这一点,因为当方法退出并且返回值的值不为空时, x将被有效地设置为null(因为它在调用之前被放置在寄存器中将x设置为null)。

我只能看到,如果你想保证在返回时(和返回值确定后)字段值的变化发生这种情况。