Does “using” statement always dispose the object?

后端 未结 3 887
渐次进展
渐次进展 2020-12-16 10:04

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 myClassInsta         


        
相关标签:
3条回答
  • 2020-12-16 10:45

    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.

    0 讨论(0)
  • 2020-12-16 10:50

    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

    0 讨论(0)
  • 2020-12-16 10:55

    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.

    0 讨论(0)
提交回复
热议问题