我非常不确定使用 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..