C# IDisposable pattern on sub-classes

Scenario: I had a base-class which was implementing the IDisposable pattern (including the GC.SuppressFinalize() part). From this class, I inherited a sub-class which also required doing some disposing stuff.

15 July 2011
Christoph Keller Christoph Keller

Today I got the following situation:

I had a base-class which was implementing the IDisposable pattern (including the GC.SuppressFinalize() part). From this class, I inherited a sub-class which also required doing some disposing stuff.

Now I asked myself how I should implement the IDisposable pattern correctly, that the dispose methods where not called twice (and throwing a ObjectDisposedException) and all my managed objects where disposed properly..

After some googling around I found a interesting blog-article:

http://reedcopsey.com/2009/03/30/idisposable-part-2-subclass-from-an-idisposable-class/

There it is clearly explained that you only have to override the Dispose(bool disposing) method (and that you do not require to implement the IDisposable interface)!

Small example:

You have the following base class:

/// <summary>
/// <see cref="IDisposable" /> interface example.
/// </summary>
public class Example : IDisposable
{
    /// <summary>
    /// Flag stating if the current instance is allready disposed.
    /// </summary>
    private bool _disposed;
 
    /// <summary>
    /// The <see cref="IDisposable" /> implementation.
    /// </summary>
    public void Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize((object)this);
    }
 
    /// <summary>
    /// Dispose method, releasing all managed resources.
    /// </summary>
    /// <param name="disposing">States if the resources should be disposed.</param>
    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
        {
            return;
        }
 
        if (disposing)
        {
            // Dispose all managed resources here.
        }
 
        _disposed = true;
    }
}

If you now want to inherit a class from the above base-class, you have to implement the sub-class in the following way:

/// <summary>
/// SubClass of a <see cref="IDisposable" /> implementing base-class.
/// </summary>
public class SubExample : Example
{
    /// <summary>
    /// Flag stating if the current instance is allready disposed.
    /// </summary>
    private bool _disposed;
 
    /// <summary>
    /// Dispose method override, releasing all managed resources of the sub-class.
    /// </summary>
    /// <param name="disposing">States if the resources should be disposed.</param>
    protected override void Dispose(bool disposing)
    {
        if (_disposed)
        {
            return;
        }
 
        if (disposing)
        {
            // Dispose all managed resources here.
        }
 
        _disposed = true;
 
        base.Dispose(disposing);
    }
}

That's it :)

If you have any suggestions or questions, feel free to write a comment! :)

Happy coding and have a nice weekend!


comments powered by Disqus