Somebody asked me today, if we have a collection that is disposable and calls dispose on all its items when the collections.Dispose method is called. You can implement such collection by inheriting from List<T> and adding Dispose logic. If you add one additional requirement that Dispose is called on the items before they are removed from the collection when the collection is cleared, you need to use Collection<T> which can be customized more than List<T>. Here is a quick custom collection that does what I am talking about.

public class DisposableCollection<T> : Collection<T>, IDisposable where T: IDisposable {
   protected virtual void Dispose(bool disposing){
         foreach(T item in this){
   public void Dispose(){

   protected override void ClearItems(){

There are two problems with this collection though: a) what happens when an item is removed from the collection by calling Remove. b) what should the user do if they hold a reference to an item residing in the collection and then call Clear, which causes the item pointed to by the reference to be disposed:

var col  = DisposableCollection<Foo>();
col.Add(new Foo());
Foo f = col[0];
// now f points to a disposed object

… maybe Clear should not be disposing the items. What do you think?