C# try{return x;}finally{x=null;}语句中到底发生了什么?
我在另一个问题中看到了这个提示,想知道是否有人能向我解释一下这到底是怎么回事C# try{return x;}finally{x=null;}语句中到底发生了什么?,c#,.net,exception-handling,C#,.net,Exception Handling,我在另一个问题中看到了这个提示,想知道是否有人能向我解释一下这到底是怎么回事 try { return x; } finally { x = null; } 我的意思是,finally子句真的在return语句之后执行吗?这个代码有多不安全?你能想出任何额外的黑客行为吗?这个try finallyhack?finally子句在return语句之后但实际从函数返回之前执行。我认为,这与线程安全性关系不大。它不是黑客攻击-finally保证始终运行,无论您在try块或catch块中执行什么操作。否
try { return x; } finally { x = null; }
我的意思是,
finally
子句真的在return
语句之后执行吗?这个代码有多不安全?你能想出任何额外的黑客行为吗?这个try finally
hack?finally子句在return语句之后但实际从函数返回之前执行。我认为,这与线程安全性关系不大。它不是黑客攻击-finally保证始终运行,无论您在try块或catch块中执行什么操作。否-在IL级别,您不能从异常处理块内部返回。它本质上将其存储在变量中,然后返回
i、 e.类似于:
int tmp;
try {
tmp = ...
} finally {
...
}
return tmp;
例如(使用反射器):
汇编至:
.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
}
这基本上是声明一个局部变量(CS$1$0000
),将值放入变量(在已处理的块内),然后在退出块后加载变量,然后返回它。Reflector将其渲染为:
private static int Test()
{
int CS$1$0000;
try
{
CS$1$0000 = SomeNumber();
}
finally
{
Foo();
}
return CS$1$0000;
}
如果
x
是一个局部变量,我看不出有什么意义,因为当方法退出时,x
将被有效地设置为null,并且返回值的值不为null(因为它是在调用将x
设置为null之前放在寄存器中的)
只有在您希望保证返回时(以及在确定返回值之后)字段值的更改时,我才能看到这样做。执行finally语句,但返回值不受影响。执行顺序为:
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";
}
}
}
这会打印“try”(因为这是返回的结果),然后打印“finally”,因为这是x的新值
当然,如果我们返回对可变对象(例如StringBuilder)的引用,那么对finally块中的对象所做的任何更改都将在返回时可见-这不会影响返回值本身(它只是一个引用)。添加到Marc Gravell和Jon Skeet给出的答案上,重要的是要注意对象和其他引用类型在返回时的行为类似,但确实存在一些差异 返回的“What”遵循与简单类型相同的逻辑:
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");
}
}
}
在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;
}
}
不同之处在于,仍然可以使用对象的属性/方法修改可变类型,如果不小心,可能会导致意外行为
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();
}
}
}
第二个关于尝试返回的问题是,在返回之后,仍然可以修改“引用”所传递的参数。只有返回值已计算并存储在等待返回的临时变量中,其他变量仍按正常方式修改。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;
}
}
}
与任何其他流构造一样,“try return finally”也有其位置,它允许编写比它编译到的结构更干净的代码。但必须小心使用,以避免出现问题。除非委托也捕获了局部变量:),否则会出现闭包,并且无法对对象进行垃圾收集,因为仍然存在引用。但我仍然不明白为什么在委托中使用局部变量,除非您打算使用它的值。这不正是ocdedio所说的吗:在计算返回值之后,在真正从funczion返回之前执行finally??“异常处理块”我认为这个场景与异常和异常处理无关。这是关于.NET如何实现finally resource guard构造的。我想问一下,visual studio中是否有任何选项可以在执行时查看为编写的C#代码生成的中间语言(IL)................如果我们返回对可变对象(例如StringBuilder)的引用,则例外情况为“如果StringBuilder对象在finally块中设置为null,则对finally块中的对象所做的任何更改都将在返回时可见”,在这种情况下,将返回一个非null对象。@尼克:这不是对对象的更改,而是对变量的更改。它根本不影响变量的前一个值所引用的对象。所以不,这不是一个例外。@Skeet这是不是意味着“return语句返回一个副本”?@prabhakaran:它在
return
语句的点处计算表达式,然后返回该值。当控件离开方法时,不会对表达式求值。
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;
}
}
}