【问题标题】:What really happens in a try { return x; } finally { x = null; } statement?在 try { return x; 中真正发生了什么? } 最后 { x = null; } 陈述?
【发布时间】:2010-09-30 02:59:33
【问题描述】:

我在另一个问题中看到了这个提示,想知道是否有人可以向我解释这到底是如何工作的?

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

我的意思是,finally 子句真的 return 语句之后执行吗?这段代码有多线程不安全?你能想到任何额外的黑客可以做的 w.r.t.这个try-finally hack?

【问题讨论】:

    标签: c# .net exception-handling


    【解决方案1】:

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

    1. return 语句执行前的代码
    2. return 语句中的表达式被计算
    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";
            }
        }
    }
    

    这会打印“try”(因为这是返回的内容),然后是“finally”,因为这是 x 的新值。

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

    【讨论】:

    • 我想问一下,Visual Studio 中是否有任何选项可以在执行时查看为编写的 C# 代码生成的中间语言(IL)......
    • 如果 StringBuilder 对象是在 finally 块中设置为 null,在这种情况下会返回一个非 null 对象。
    • @Nick:这不是对 object 的更改 - 这是对 variable 的更改。它根本不会影响变量的先前值所引用的对象。所以不,这不是个例。
    • @Skeet 这是否意味着“return 语句返回一个副本”?
    • @prabhakaran:它在return 语句处计算表达式,并将返回该值。当控制离开方法时,表达式被评估。
    【解决方案2】:

    否 - 在 IL 级别,您不能从异常处理块内部返回。它本质上将它存储在一个变量中,然后返回

    即类似于:

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

    例如(使用反射器):

    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
    }
    

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

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

    【讨论】:

    • 这不正是ocdedio所说的:finally是在计算完返回值之后,真正从函数返回之前执行的???
    • "异常处理块" 我认为这个场景与异常和异常处理无关。这是关于 .NET 如何实现 finally resource 保护结构的。
    【解决方案3】:

    finally 子句在 return 语句之后但在函数实际返回之前执行。我认为这与线程安全无关。这不是 hack - finally 保证始终运行,无论您在 try 块或 catch 块中做什么。

    【讨论】:

      【解决方案4】:

      除了 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();
              }
          }
      }
      

      关于 try-return-finally 要考虑的第二件事是“通过引用”传递的参数在返回后仍然可以修改。只有返回值被评估并存储在一个等待返回的临时变量中,任何其他变量仍然以正常方式修改。 out 参数的约定甚至可以在 finally 阻塞之前无法实现。

      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”有它的位置,并且可以让代码看起来更简洁,而不是编写它实际编译成的结构。但必须小心使用,以免出现问题。

      【讨论】:

        【解决方案5】:

        如果x 是局部变量,我看不出重点,因为当方法退出并且返回值的值不为 null 时,x 无论如何都会被有效地设置为 null(因为它是在调用之前放置在寄存器中以将x 设置为null)。

        如果您想保证在返回时(以及在确定返回值之后)更改字段的值,我只能看到这样做。

        【讨论】:

        • 除非局部变量也被委托捕获:)
        • 然后有一个闭包,对象不能被垃圾回收,因为还有一个引用。
        • 但是我仍然不明白为什么你会在委托中使用本地变量,除非你打算使用它的值。
        猜你喜欢
        • 2019-05-07
        • 2012-06-12
        • 2013-11-23
        • 2016-05-02
        • 1970-01-01
        • 2012-10-27
        • 2012-06-15
        • 2019-07-02
        • 1970-01-01
        相关资源
        最近更新 更多