How to Dispose an object using Dispose method

This question already has an answer here:

  • Proper use of the IDisposable interface 18 answers

  • The dispose method works together with the using statement. It will automatically called if a using block is closed.

    class A : IDisposable
    {
        public void Dispose()
        {
            // Dispose
        }
    }
    
    using (A a = new A())
    {
    
    }
    

    You also don't need to override the method, because it is defined in an interface.

    In any case using is leaved, dispose will be called.

    链接地址: http://www.djcxy.com/p/54488.html

    上一篇: Finalizer和IDisposable

    下一篇: 如何使用Dispose方法处理对象