返回在使用块的中间 [英] returning in the middle of a using block

查看:135
本文介绍了返回在使用块的中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是这样的:

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

我相信这不是一个return语句一个合适的地方,是吗?

I believe it is not a proper place for a return statement, is it?

推荐答案

随着其他几个人都在普通指出,这是没有问题的。

As several others have pointed out in general this is not a problem.

这会导致你的问题的唯一情况是,如果你在一个using语句的中间返回,另外返回使用变量。不过话说回来,这也将导致你问题,即使你没有回来,只是保持一个变量的引用。

The only case it will cause you issues is if you return in the middle of a using statement and additionally return the in using variable. But then again, this would also cause you issues even if you didn't return and simply kept a reference to a variable.

using ( var x = new Something() ) { 
  // not a good idea
  return x;
}

同样糟糕​​

Something y;
using ( var x = new Something() ) {
  y = x;
}

这篇关于返回在使用块的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆