In the following code, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With