Please enable Javascript to correctly display the contents on Dot Net Tricks!

Difference Between Finalize and Dispose Method

  Author : Shailendra Chauhan
Updated On : 30 Sep 2015
Total Views : 100,764   
Support : All .Net Framework
 

.NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources like files, database connections, COM etc. This article helps you to understand the difference between Finalize and Dispose method.

Difference between Dispose & Finalize Method
Dispose
Finalize
It is used to free unmanaged resources like files, database connections etc. at any time.
It can be used to free unmanaged resources (when you implement it) like files, database connections etc. held by an object before that object is destroyed.
Explicitly, it is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.
Internally, it is called by Garbage Collector and cannot be called by user code.
It belongs to IDisposable interface.
It belongs to Object class.
It's implemented by implementing IDisposable interface Dispose() method.
It's implemented with the help of destructor in C++ & C#.
There is no performance costs associated with Dispose method.
There is performance costs associated with Finalize method since it doesn't clean the memory immediately and called by GC automatically.

Example for implementing the dispose method

public class MyClass : IDisposable
{
 private bool disposed = false;
 
 //Implement IDisposable.
 public void Dispose()
 {
 Dispose(true);
 GC.SuppressFinalize(this);
 }

 protected virtual void Dispose(bool disposing)
 {
 if (!disposed)
 {
 if (disposing)
 {
 // TO DO: clean up managed objects
 }
 
 // TO DO: clean up unmanaged objects

 disposed = true;
 }
 }
}

Example for implementing Finalize method

If you want to implement Finalize method, it is recommended to use Finalize and Dispose method together as shown below:

// Using Dispose and Finalize method together
public class MyClass : IDisposable
{
 private bool disposed = false;
 
 //Implement IDisposable.
 public void Dispose()
 {
 Dispose(true);
 GC.SuppressFinalize(this);
 }

 protected virtual void Dispose(bool disposing)
 {
 if (!disposed)
 {
 if (disposing)
 {
 // TO DO: clean up managed objects
 }
 
 // TO DO: clean up unmanaged objects
 
 disposed = true;
 }
 }
 
 //At runtime C# destructor is automatically Converted to Finalize method
 ~MyClass()
 {
 Dispose(false);
 }
}

Note

  1. It is always recommended to use Dispose method to clean unmanaged resources. You should not implement the Finalize method until it is extremely necessary.

  2. At runtime C#, C++ destructors are automatically Converted to Finalize method. But in VB.NET you need to override Finalize method, since it does not support destructor.

  3. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.

  4. A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor 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.

What do you think?

I hope you will enjoy the tips while programming with .NET Framework. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri     6:30 AM-7:30 AM IST

3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+