Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / Visual Basic

Simplified Resource Management with the DisposableCollection Class

Rate me:
Please Sign up or sign in to vote.
4.42/5 (8 votes)
26 Sep 20044 min read 55.3K   367   17  
This article demonstrates how a strongly typed collection of IDisposable objects can be used to simplify the management of multiple resources in .NET.
////////////////////////////////////////////////////////////////////////////////////
// DisposableCollection.cs
//
// By Scott McMaster (smcmaste@hotmail.com)
// 09/24/2004
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;

namespace ScottMcMaster
{
	/// <summary>
	/// Implement a collection of Disposable objects that is itself
	/// disposable, the goal being to provide a convenient programming model
	/// for working with multiple disposable objects at the same time.
	/// </summary>
	public sealed class DisposableCollection : CollectionBase, IDisposable
	{
		private bool disposed;

		public DisposableCollection()
		{
			disposed = false;
		}

		~DisposableCollection()
		{
			Dispose(false);
		}

		private void ThrowIfDisposed()
		{
			if( disposed )
			{
				throw new ObjectDisposedException( this.GetType().Name );
			}
		}

		private void Dispose(bool disposing)
		{
			lock(this)
			{
				// Check to see if Dispose has already been called.
				if(!this.disposed)
				{
					if( disposing )
					{
						// Dispose managed resources
						foreach( IDisposable target in List )
						{
							// Watch out for somebody adding the DisposableCollection to itself
							// lest we overflow the stack.
							if( target != this )
							{
								target.Dispose();
							}
						}
						List.Clear();
					}
				}
				disposed = true;
			}
		}

		#region CollectionBase Implementation

		public IDisposable this[ int index ]  
		{
			get  
			{
				ThrowIfDisposed();
				return( (IDisposable) List[index] );
			}
			set  
			{
				ThrowIfDisposed();
				List[index] = value;
			}
		}

		public int Add( IDisposable value )  
		{
			ThrowIfDisposed();
			return( List.Add( value ) );
		}

		public int IndexOf( IDisposable value )  
		{
			ThrowIfDisposed();
			return( List.IndexOf( value ) );
		}

		public void Insert( int index, IDisposable value )  
		{
			ThrowIfDisposed();
			List.Insert( index, value );
		}

		public void Remove( IDisposable value )  
		{
			ThrowIfDisposed();
			List.Remove( value );
		}

		public bool Contains( IDisposable value )  
		{
			ThrowIfDisposed();
			return( List.Contains( value ) );
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			Dispose(true);
			// Take yourself off the Finalization queue 
			// to prevent finalization code for this object
			// from executing a second time.
			GC.SuppressFinalize(this);
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United States United States
I have over 10 years of full-lifecycle software development experience on a variety of large projects. I have a B.S. in Math from the University of Nebraska-Lincoln, a Masters in Software Engineering from Seattle University, and a Masters in Computer Science from the University of Maryland. I specialize in building and installing tools, frameworks, and processes that improve developer productivity and product quality. I am currently based in the Seattle area.

Comments and Discussions