Question

Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.:

using (var myClassInstance = new MyClass())
{
    // ...
    return;
}

or

using (var myClassInstance = new MyClass())
{
    // ...
    throw new UnexplainedAndAnnoyingException();
}
Was it helpful?

Solution

Yes, that's the whole point. It compiles down to:

SomeDisposableType obj = new SomeDisposableType();
try
{
    // use obj
}
finally
{
    if (obj != null) 
        ((IDisposable)obj).Dispose();
}

Be careful about your terminology here; the object itself is not deallocated. The Dispose() method is called and, typically, unmanaged resources are released.

OTHER TIPS

No it doesn't.

But that's not the fault of using statement though. It's because how the finally blocks are handled by CLR. There ARE some cases that finally blocks will not execute. If you have an unhandled exception and if the CLR thinks that executing more code will lead to more errors, it will not execute Dispose method (because it will not execute the finally block which Dispose method is compiled down to..). Therefore, be very careful and don't put your life into the execution of Dispose method.

The other cases which can cause Dispose method not being executed can be listed as:

  • Environment.FailFast

  • OutOfMemoryExceptionand StackOverflowException

  • Killing the process

  • Power loss

If the object implements IDisposable, it will be called.

From using Statement (C# Reference) by MSDN

Defines a scope, outside of which an object or objects will be disposed.

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top