What is the difference between using IDisposable vs a destructor in C#?

A finalizer (aka destructor) is part of garbage collection (GC) – it is indeterminate when (or even if) this happens, as GC mainly happens as a result of memory pressure (i.e. need more space). Finalizers are usually only used for cleaning up unmanaged resources, since managed resources will have their own collection/disposal.

Hence IDisposable is used to deterministically clean up objects, i.e. now. It doesn’t collect the object’s memory (that still belongs to GC) – but is used for example to close files, database connections, etc.

There are lots of previous topics on this:

Finally, note that it is not uncommon for an IDisposable object to also have a finalizer; in this case, Dispose() usually calls GC.SuppressFinalize(this), meaning that GC doesn’t run the finalizer – it simply throws the memory away (much cheaper). The finalizer still runs if you forget to Dispose() the object.

Leave a Comment