Difference between destructor, dispose and finalize method

C#.NetDestructorDisposeFinalize

C# Problem Overview


I am studying how garbage collector works in c#. I am confused over the use of Destructor, Dispose and Finalize methods.

As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection in the way mentioned in the destructor method which cannot be called explicitly on the instances of the class.

The Dispose method is meant to provide the user to control the garbage collection. The Finalize method frees the resources used by the class, but not the object itself.

I am not sure if I understand it the right way. Please clarify the doubts. Any further links or guides are welcome.

C# Solutions


Solution 1 - C#

Destructor implicitly calls the Finalize method, they are technically the same. Dispose is available with objects that implement the IDisposable interface.

You may see : Destructors C# - MSDN

> The destructor implicitly calls Finalize on the base class of the > object.

Example from the same link:

class Car
{
    ~Car()  // destructor
    {
        // cleanup statements...
    }
}

The Destructor's code is implicitly translated to the following code:

protected override void Finalize()
{
    try
    {
        // Cleanup statements...
    }
    finally
    {
        base.Finalize();
    }
}

Your understanding for the Destructor is right:

From MSDN

> The programmer has no control over when the destructor is called > because this is determined by the garbage collector. The garbage > collector checks for objects that are no longer being used by the > application. If it considers an object eligible for destruction, it > calls the destructor (if any) and reclaims the memory used to store > the object. Destructors are also called when the program exits. It is > possible to force garbage collection by calling Collect, but most of > the time, this should be avoided because it may create performance > issues.

Solution 2 - C#

In C# terms, a destructor and finalizer are basically interchangeable concepts, and should be used to release unmanaged resources when a type is collected, for example external handles. It is very rare that you need to write a finalizer.

The problem with that is that GC is non-deterministic, so the Dispose() method (via IDisposable) makes it possible to support deterministic cleanup. This is unrelated to garbage collection, and allows the caller to release any resources sooner. It is also suitable for use with managed resources (in addition to unmanaged), for example if you have a type that encapsulates (say) a database connection, you might want disposing of the type to release the connection too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVictor MukherjeeView Question on Stackoverflow
Solution 1 - C#HabibView Answer on Stackoverflow
Solution 2 - C#Marc GravellView Answer on Stackoverflow