Does "using" statement always dispose the object?
Asked Answered
G

3

35

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();
}
Griffith answered 28/6, 2013 at 4:36 Comment(0)
C
43

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.

Canst answered 28/6, 2013 at 4:38 Comment(1)
There are some cases that finally block will not execute as explained in my answer.Fare
F
16

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

Fare answered 13/7, 2018 at 8:47 Comment(3)
Power loss ... really ?Scrag
Really. Electrical power won't wait for one's badly implemented Dispose method to finish keeping the persistence layer in a good state for example.Fare
@Scrag Yes. Power loss. :)Henryhenryetta
Z
11

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.

Zygophyte answered 28/6, 2013 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.