GOTO使用块内,将对象得到处理? [英] GOTO inside using block, will the object get disposed?

查看:164
本文介绍了GOTO使用块内,将对象得到处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常不确定使用 GOTO 内的使用块。

I'm quite unsure about using goto inside an using block.

例如:

using(stream s = new stream("blah blah blah"));
{
    //do some stuff here

    if(someCondition) goto myLabel;
}

现在,如果 someCondition 是真实的code执行将上移至 myLabel ,但是,请问对象得到处理?

Now if someCondition is true the code execution will move on to myLabel, but, will the the object get disposed?

我已经看到了一些pretty的很好的问题在这里就这个话题,但他们都在谈论不同的事情。

I've seen some pretty good questions here on this topic but they all talk about different things.

推荐答案

using语句基本上是一个try-finally块和包裹在一个简单的语句中的Dispose模式。

The using statement is essentially a try-finally block and a dispose pattern wrapped up in one simple statement.

using (Font font1 = new Font("Arial", 10.0f))
{
    //your code
}

时相当于

Font font1 = new Font("Arial", 10.0f);
try
{
     //your code
}
finally
{
     //Font gets disposed here
}

因此​​,从试块任何跳跃,无论是抛出一个异常,使用goto(不干净!)及TC。将执行对象的处理在这最后块正在使用。

Thus, any jump from the "try-block", be it throwing an exception, the use of goto (unclean!) &tc. will execute the Disposal of the object being used in that "finally" block..

这篇关于GOTO使用块内,将对象得到处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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