returning in the middle of a using block

后端 未结 7 1062
一生所求
一生所求 2020-11-27 15:08

Something like:

using (IDisposable disposable = GetSomeDisposable())
{
    //.....
    //......
    return Stg();
}

I believe it is not a p

相关标签:
7条回答
  • 2020-11-27 15:34

    That is totally acceptable. A using statement ensures the IDisposable object will be disposed no matter what.

    From MSDN:

    The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler.

    0 讨论(0)
提交回复
热议问题