I know a using
block ensures that the IDisposable
object being "used" will be disposed at the end of the block - but does it guarantee that the object will not be disposed before then?
In other words, if I have:
using (new DisposableObject()) {
// blah blah
}
can I be sure that the DisposableObject
will not be disposed until the end of the block, or might the garbage collector detect that I have no handle to it inside the block, and dispose it right away?
Your object will only be disposed once and will not be GC'd before the end of the using block.
The specification guarantees that when you have using (expression) statement
and the type of expression is a nullable type (call it ResourceType
) then it is expanded into:
{
ResourceType resource = expression;
try {
statement;
}
finally {
if (resource != null) ((IDisposable)resource).Dispose();
}
}
Dispose
will be called once at the end of the block and there is a local generated that keeps a reference to your disposable object. The local prevents the object from being GC'd before the end of the using block.