returning in the middle of a using block

后端 未结 7 1098
一生所求
一生所求 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:20

    It's absolutely fine - no problem at all. Why do you believe it's wrong?

    A using statement is just syntactic sugar for a try/finally block, and as Grzenio says it's fine to return from a try block too.

    The return expression will be evaluated, then the finally block will be executed, then the method will return.

提交回复
热议问题