Curious C# using statement expansion

前端 未结 5 978
一个人的身影
一个人的身影 2021-01-17 07:31

I\'ve run ildasm to find that this:

    using(Simple simp = new Simple())
    {
        Console.WriteLine(\"here\");
    }

generates IL cod

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 07:57

    It appears that your comment:

    "If there is some fear that some intervening steps might come between the Simple constructor and the beginning of the try block, then that would really be a problem because then an exception might be thrown that would prevent the finally block from executing at all."

    is possibly dead on. See:

    Atomicity & Asynchronous Exception Failures

    I also want to note the issue(s) with WCF and using:

    Avoiding Problems with the Using Statement and WCF Service Proxies which references:

    Avoiding Problems with the Using Statement

提交回复
热议问题