.NET Tip: Implement IDisposable to Ensure Your Objects Clean Up After Themselves

This week, I'm passing along a tip I ran across several months back when trying to track down some resources that weren't being released as I expected. Normally, you can let .NET's garbage collection take care of cleaning up after your objects, unless you are using unmanaged resources such as a database connection. The .NET framework provides the IDisposable interface for just such a case.

Even when your class does implement IDisposable, there is no guarantee that the Dispose() method will be called, so you should always place a call to Dispose() in your object's finalizer (destructor). This is needed because of how garbage collection works. When the garbage collector runs, any objects without finalizers are simply deleted from memory, providing the potential for resource leakage. Any objects with a finalizer are left in memory and their finalizer is executed. This is where you have the opportunity to safely release any resources your object uses. So, take a look at how you implement IDisposable.

public class GoodResourceCitizen : IDisposable
   private bool _IsDisposed = false;


   public void Dispose()
      // Tell the garbage collector not to call the finalizer
      // since all the cleanup will already be done.

   protected virtual void Dispose(bool IsDisposing)
      if (_IsDisposed)

      if (IsDisposing)
         // Free any managed resources in this section

      // Free any unmanaged resources in this section
      _IsDisposed = true;

The overloaded virtual version of Dispose() allows derived classes to easily call the base class to properly release resources. The finalizer simply calls the overloaded Dispose() method. The regular Dispose() method calls the overloaded version and then tells the garbage collector that there is no need to call the finalizer for this object. You do need to be careful of what code you put in the Dispose() method. The only thing you should do in Dispose() is clean up and free resources. You should not be calling other methods or referencing any other objects. You do not know in what order objects are disposed, so you may be attempting to access a dead object. Worse yet, you could resurrect the object, but because it has already be disposed once, the garbage collector will ignore it.

Implementing IDisposable is a good practice to get into, especially for any classes that use unmanaged resources. Although the .NET garbage collector does a good job of cleaning up resources, you have the responsibility to do whatever you can to aid it by making your objects good resource citizens. You also will end up with more stable and maintainable classes.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.


  • One Small Issue

    Posted by Ragin' Geek on 04/04/2012 01:52pm

    looks like you have a typo in line 15, it should be GC.SuppressFinalize(this);

  • Nice article

    Posted by kirants on 08/24/2007 01:45am

    useful tip. Thought the following MSDN magazine article delves into more details on the same tip. Perhaps would merit adding a reference to this link in your article ? http://msdn.microsoft.com/msdnmag/issues/07/07/CLRInsideOut/default.aspx

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date