How to implement IDisposable properly

Every so often I write a class with disposable members, so it needs to implement IDisposable. I always copy and paste from one of my other classes, so I figured it would be useful to share the pattern of IDisposable alone in a simple class that does nothing else.

This example has a single disposable member, a BackgroundWorker, only because that’s what my latest class happened to use. Actually I wouldn’t recommend using this component anymore. The Task-based Asynchronous Pattern (TAP) is much easier to use once you get used to it, but this was a .Net 3.5 project, and a BackgroundWorker was the easiest way to do some funky progress stuff easily in its ProgressChangedEventHandler event handler…

Also there are other reasons BackgroundWorker is a bad example… Namely that it doesn’t need to be disposed and also, how do you know when to dispose it? (My “real” class is used at system start-up. It creates the worker only when the method is called; then gets disposed immediately after. The problem obviously is, the calling code can’t use it in a using statement because it will free the worker before the method can use it. I raised an event when its work was complete. Not shown here.) Anyway, this is just an example. OK? Pretend the BackgroundWorker is some other managed disposable type, as it doesn’t really matter as far as the pattern is concerned.

I’m not going to explain the code. Anybody who can read should be able to understand it.

using System;
using System.ComponentModel;

namespace Example
    public class Disposable : IDisposable
        private BackgroundWorker worker;
        private bool disposed;


        public void DoSomething()
            worker = new BackgroundWorker();

            // Examples of using BackgroundWorker not shown.
            // You could set up your event handlers here.

        protected virtual void Dispose(bool disposing)
            if (!disposed)
                if (disposing)
                    // Release managed resources.
                    if (worker != null)
                        worker = null;

                // Free unmanaged resources
                // Nothing unmanaged to free in this example.

                disposed = true;

        public void Dispose()

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s