eng_mazzy
eng_mazzy

Reputation: 1049

IDisposable interface and implement deconstruct method

I'm studying C# theory and I don't have some part of theory clear. When I have to call in a class the destructor method I MUST implement IDisposable interface?I mean implementing interface is strictly correlated with destructor ?

Upvotes: 0

Views: 442

Answers (2)

supercat
supercat

Reputation: 81115

A "destructor" in C# does not "destroy" an object. To the contrary, it directs the compiler to generate something called a "finalizer", whose existence indicates to the garbage collector that when an object is found to have been abandoned, it should be notified of that fact (by running the code within the body of the destructor) before it, or any object to which it holds a direct or indirect reference, is destroyed.

Rarely should there be any need for an object to do something in response to being abandoned. If an object has requested one or more outside entities to do something on its behalf until further notice (e.g. it has requested and been granted exclusive access to a file) it should notify such entities as soon as their services are no longer needed. This is best accomplished by having the object implement IDisposable.Dispose with a method that will put its affairs in order by carrying out such notifications, and by having the user of such object notify it when it is no longer needed, before abandoning it, by calling the aforementioned IDisposable.Dispose. Finalizers generally only exist as a back-stop in case objects get wrongfully abandoned without having been first notified that they were no longer needed.

From a pedagogical standpoint, I would defer teaching anything about destructors beyond the fact that there exist ways by which programs which wrongfully abandon objects without first calling IDisposable.Dispose can be made to 'kinda sorta' work. Having an improperly written destructors/finalizers will often turn a program which would have failed in a clear and obvious fashion into one which usually works, but occasionally malfunctions in bizarre and inexplicable ways. Come to think of it, even properly written destructors/finalizers can have much the same effect, though with the failures being less frequent and less bizarre. Any efforts spent by a novice learning how to write finalizers would IMHO be better spent learning how not to need them.

Upvotes: 2

Daniel Pratt
Daniel Pratt

Reputation: 12077

No, if you implement a 'finalizer' in your C# class, you are not required to also implement the IDisposable interface. It is, however, considered to be a "best practice" to do so.

Why? Well, the typical reason to implement a finalizer in a class is to reclaim scarce resources 'owned' by instances of the class. Perhaps the class manages a database connection or a file or resource handle. Since .NET uses garbage collection to manage object lifetimes, there is no guarantee as to when a finalizer will be run (there are ways to force finalizers to run, but there are definite drawbacks to doing this).

The IDisposable interfaces provides a way for consumers of a class to more carefully control the use of the resource(s) managed by the class. When you implement the IDisposable interface, you can consider a finalizer to be a fail-safe mechanism in the event the class is not 'disposed' properly.

Upvotes: 1

Related Questions