开发者

Will an implicit variable in a using clause be garbage collected?

In the following cod开发者_StackOverflow社区e, does the underlying code contain a hard reference to the unnamed variable instance of type Foo, or is the item vulnerable to garbage collection?

using(new Foo())
{
    // Something done here.
}

The collected item is just a semaphore type object that performs some reference counting on resources so it isn't being referenced in the code block.


The using clause creates a hidden locally-scoped variable holding the object (this variable is used by the generated finally clause).

This variable prevents the object from being GC'd.

You can see this variable in the spec.


using(new Foo())

this anonymous instance of Foo will go out of scope after the using block and may be garbage collected then.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜