Click here to Skip to main content
13,355,726 members (65,952 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 15 Mar 2007

AutoDisposer utility class

, 15 Mar 2007
Rate this:
Please Sign up or sign in to vote.
In this article you see how a simple class like the AutoDisposer can make programming a lot easier.

Screenshot - AutoDisposer.gif


There are a lot of classes (in the .NET framework or perhaps your own classes) that implement the interface IDisposable in order to release allocated unmanaged resources. When a user instantiates such a class then he must call the method 'Dispose' when he is done with that instance of the class. Following this pattern can easily result in a lot of code, because this pattern should be used with a try-finally block (or a using statement). This article describes the AutoDisposer utility class that can reduce a lot of this code, making your code more readable.


Consider the following code (samplecode1):

private void ClaimResourcesOld()
    ManualResetEvent resetEvent = new ManualResetEvent(true);
        Brush drawingBrush = new SolidBrush(Color.FromArgb(50, Color.Purple));

Using two disposable objects leads to a nesting of try-finally blocks. Note that one could also write the same code with nested using statements, which reduces the amount of code already somewhat but the nested structure is still required. Isn't there a way that one could write code with the same functionality without the nesting and with less code? Yes there is; with a small utility class called AutoDisposer.

The AutoDisposer class

The AutoDisposer class is a small utility class that keeps track of a list of disposable objects. The class itself implements IDisposable and in the 'Dispose' of the class the tracked elements are disposed. The interface of the class is:

public sealed class AutoDisposer : IDisposable
    public AutoDisposer() : this(false) { }
    public AutoDisposer(bool disposeReverse) { m_DisposeReverse = 
        disposeReverse; }

    public void Add(object obj) { ... }

The user must create an instance of the AutoDisposer class and add disposable objects through the 'Add' method. When disposing the instance of the AutoDisposer class all the tracked disposable objects are disposed in the order that they are added. This order can also specified by the user when the overloaded second constructor of the AutoDisposer class is used.

When searching on the internet I found the article Writing Better Software: Managing Unmanaged Resources which also talks about an AutoDisposer class, which has a stack-based implementation for tracking disposable objects. The AutoDisposer class of this article differs on the following issues

  • All types of objects can be added, not only objects of type IDisposable
  • The disposable objects are tracked with a list, where the used can specify the order in which to dispose the tracked objects
  • The class also deals with disposable null objects, the user doesn't have to check the validness of the disposable object when adding it
This results in an easier to use utility class.

Using the code

When using the AutoDisposer class samplecode1 can be changed to the following code (samplecode2):

private void ClaimResourcesNew()
    using (AutoDisposer autoDisposer = new AutoDisposer())
        ManualResetEvent resetEvent = new ManualResetEvent(true);
        Brush drawingBrush = new SolidBrush(Color.FromArgb(50, Color.Purple));

As you can see, this reduces the code drastically and makes the code more readable.


Reading this article should have made you aware how a small utility class as the AutoDisposer can make your life easier. Any comments or questions post below or e-mail them.


Version 1.0 - This is the first version of this article.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDisposableHelper Pin
Drew Noakes29-Apr-07 15:04
memberDrew Noakes29-Apr-07 15:04 
QuestionUnmanaged - Managed Pin
stixoffire22-Mar-07 11:16
memberstixoffire22-Mar-07 11:16 
AnswerRe: Unmanaged - Managed Pin
CIDev22-Mar-07 13:01
memberCIDev22-Mar-07 13:01 
GeneralRe: Unmanaged - Managed Pin
stixoffire23-Mar-07 8:31
memberstixoffire23-Mar-07 8:31 
GeneralRe: Unmanaged - Managed Pin
CIDev26-Mar-07 5:09
memberCIDev26-Mar-07 5:09 
GeneralNice idea Pin
Steve Hansen20-Mar-07 4:50
memberSteve Hansen20-Mar-07 4:50 
QuestionAll types of objects can be added? Pin
Colin Angus Mackay16-Mar-07 4:53
mvpColin Angus Mackay16-Mar-07 4:53 
AnswerRe: All types of objects can be added? Pin
meeuwene19-Mar-07 0:43
membermeeuwene19-Mar-07 0:43 
GeneralRe: All types of objects can be added? Pin
Colin Angus Mackay19-Mar-07 2:32
mvpColin Angus Mackay19-Mar-07 2:32 
GeneralRe: All types of objects can be added? Pin
Steve Solomon20-Mar-07 6:42
memberSteve Solomon20-Mar-07 6:42 
Generalmultiple objects in using Pin
knew15-Mar-07 10:13
memberknew15-Mar-07 10:13 
GeneralRe: multiple objects in using Pin
meeuwene15-Mar-07 13:00
membermeeuwene15-Mar-07 13:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 15 Mar 2007
Article Copyright 2007 by meeuwene
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid