Difference between Destructor, Dispose and Finalize methods

Here, I have given a brief description of these 3 methods.

1. Destructor
They are special methods that contains clean up code for the object. You can not call them explicitly in your code as they are called implicitly by GC. In C# they have same name as the class name preceded by the "~" sign. Like-

Class MyClass
{

~MyClass()
{
.....
}
}

In VB.NET, destructors are implemented by overriding the Finalize method of the System.Object class.

2. Dispose
These are just like any other methods in the class and can be called explicitly but they have a special purpose of cleaning up the object. In the dispose method we write clean up code for the object. It is important that we freed up all the unmanaged recources in the dispose method like database connection, files etc.
The class implementing dispose method should implement IDisposable interface.A Dispose method should call the GC.SuppressFinalize method for the object it is disposing if the class has desturctor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.
Reference: http://msdn2.microsoft.com/en-us/library/aa720161(VS.71).aspx

3. Finalize
A Finalize method acts as a safeguard to clean up resources in the event that your Dispose method is not called. You should only implement a Finalize method to clean up unmanaged resources. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically. Finalize method is called by the GC implicitly therefore you can not call it from your code.

Note: In C#, Finalize method can not be override, so you have to use destructor whose internal implementation will override the Finalize method in MSIL.But in the VB.NET, Finalize method can be override because it does support destructor method.

10 comments:

John Fuex said...

Great article! I haven't seen this concept expressed so succinctly and clearly anywhere else.

Anonymous said...

Nice article. Easy to read and understand.

Anonymous said...

really nice article..

Anonymous said...

Agree with John here...very small and straightforward...

Thanks for such a nice article.

Anonymous said...

Very nice but I think finalize part was little comfusing as it didnt clear the confusion that which one is use for managed and unmanaged code

Kenzi said...

Destructors don't exist in C#.

You described a C# finalizer. In C++, a destructor runs when the object is deleted or falls out of the scope of the stack. In C#, the finalizer is similar, except that it is called at an unspecified time in the future.

Unfortunately, Microsoft confused everyone by using 'destructor' and 'finalizer' interchangeably. They're the same thing; see this article:
http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

Anonymous said...

Still have no idea which to use and when. Microsoft made it very confusing. Again.

lingmaaki said...

Finalize()

Finalize() is called by the Garbage Collector before an object that is eligible for collection is reclaimed. Garbage collector will take the responsibility to deallocate the memory for the unreferenced object. The Garbage Collector calls this method at some point after there are no longer valid references to that object in memory.

http://net-informations.com/faq/framework/finalize-dispose.htm

yang

bharat1 said...

Maybe you forgot to put word 'NOT' in --- VB.NET, Finalize method can be override because it does (NOT) support destructor method

难得片源小语种学习 said...
This comment has been removed by a blog administrator.