Difference Between Finalize and Dispose Method

19 Oct 2023
Intermediate
309K Views
6 min read  

.NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources like files, database connections, COM etc. This article will help you to understand the difference between Finalize and Dispose method. Check out our .NET Certification Training to get an edge over the competition.

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.

Read More Articles Related to .net
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. We'll try to provide you Best .NET Training Course in our articles.

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Learn to Crack Your Technical Interview

Accept cookies & close this