Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When would I implement IDispose on a class as opposed to a destructor? I read this article, but I'm still missing the point.

My assumption is that if I implement IDispose on an object, I can explicitly 'destruct' it as opposed to waiting for the garbage collector to do it. Is this correct?

Does that mean I should always explicitly call Dispose on an object? What are some common examples of this?

like image 591
Jordan Parmer Avatar asked Dec 03 '08 23:12

Jordan Parmer


People also ask

When should you use IDisposable?

You should implement IDisposable when your class holds resources that you want to release when you are finished using them. Show activity on this post. When your class contains unmanaged objects, resources, opened files or database objects, you need to implement IDisposable .

What is difference between Dispose () method and destructor ()?

Dispose is available with objects that implement the IDisposable interface. The destructor implicitly calls Finalize on the base class of the object. Example from the same link: class Car { ~Car() // destructor { // cleanup statements... } }

What is IDisposable used for?

Typically, types that use unmanaged resources implement the IDisposable or IAsyncDisposable interface to allow the unmanaged resources to be reclaimed. When you finish using an object that implements IDisposable, you call the object's Dispose or DisposeAsync implementation to explicitly perform cleanup.

Why is IDisposable needed c #)?

The use of IDisposable is a pattern. It's so important that it gets its own language construct (the using block), but it's just a pattern. The difference with a destructor, is that in . NET, the destructor is non-deterministic.


1 Answers

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:

  • deterministic finalization
  • disposing objects
  • using block
  • resources

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.

like image 52
Marc Gravell Avatar answered Oct 08 '22 20:10

Marc Gravell