Best way to dispose a list

I don't agree that you shouldn't do anything if you don't need the objects in the list anymore. If the objects implement the interface System.IDisposable then the designer of the object thought that the object holds scarce resources.

If you don't need the object anymore and just assign null to the object, then these scarce resources are not freed until the garbage collector finalizes the object. In the mean time you can't use this resource for something else.

Example: Consider you create a bitmap from a file, and decide you don't need neither the bitmap, nor the file anymore. Code could look like follows:

using System.Drawing;
Bitmap bmp = new Bitmap(fileName);
... // do something with bmp until not needed anymore
bmp = null;
File.Delete(fileName); // EXCEPTION, filename is still accessed by bmp.

The good method would be:

bmp.Dispose();
bmp = null;
File.Delete(fileName);

The same accounts for objects in a list, or any collection. All objects in the collection that are IDisposable should be disposed. Code should be like:

private void EmptySequence (IEnumerable sequence)
{   // throws away all elements in the sequence, if needed disposes them
    foreach (object o in sequence)
    {
        // uses modern pattern-matching
        if (disposableObject is IDisposable disposable)
        {
            disposable.Dispose();
        }
    }
}

Or if you want to create an IEnumerable extension function

public static void DisposeSequence<T>(this IEnumerable<T> source)
{
    foreach (IDisposable disposableObject in source.OfType(System.IDisposable))
    {
        disposableObject.Dispose();
    };
}

All lists / dictionaries / read only lists / collections / etc can use these methods, because they all implement IEnumerable interface. You can even use it if not all items in the sequence implement System.IDisposable.


Best idea is to leave it to the garbage collector. Your foreach will do nothing since only the reference will be set to null not the element in the list. Setting the list to null could in fact cause garbage collection to occur later than it could have (see this post C#: should object variables be assigned to null?).


Another idea for this post... If you were wanting to ensure that all members of a collection are properly disposed, you could use the following extension method:

public static void DisposeAll(this IEnumerable set) {
    foreach (Object obj in set) {
        IDisposable disp = obj as IDisposable;
        if (disp != null) { disp.Dispose(); }
    }
}

This looks through the collection for any member that implements IDisposableand disposing of it. From your executing code, you could clean up the list like this:

usersCollection.DisposeAll();
usersCollection.Clear();

This will ensure that all members get the chance to release resources and the resulting list is empty.


Firstly, you cannot "dispose" a list since it isn't IDisposable, and you can't force it to be collected since that isn't how C# works. Typically you would do nothing here. So when might we need to do anything?

  • If it is a method variable, and your method is going to exit in a moment, don't do anything: let the GC worry about it at some point after the method has existed.
  • If it is a field (instance variable), and the object is going to go out of scope in a moment, don't do anything: let the GC worry about it at some point after the instance is unreachable.

The only time you need to anything is if it is a field (or captured variable / iterator block variable / etc) and the instance (/delegate/iterator) is going to live a long while longer - then perhaps set the list field to null. Note, however, that if any other code still has a reference to the list then everything will still be reachable.