Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Difference Between Finalize and Dispose Method

Difference Between Finalize and Dispose Method

28 May 2024
Intermediate
323K Views
6 min read
Learn with an interactive course and practical hands-on labs

Free ASP.NET Core Course

Finalize Vs. Dispose Method: An Overview

.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 methods. 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 that is implementing the disposal method must have implemented an IDisposable interface.
Internally, it is called by Garbage Collector and cannot be called by user code.
It belongs to the IDisposable interface.
It belongs to the Object class.
It's implemented by implementing the IDisposable interface Dispose() method.
It's implemented with the help of destructor in C++ & C#.
There are no performance costs associated with the Dispose method.
There are performance costs associated with the Finalize method since it doesn't clean the memory immediately and is called by GC automatically.

Example of implementing the disposal 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 of implementing Finalize method

If you want to implement the Finalize method, it is recommended to use the 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 the 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 the Finalize method since it does not support the 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 that has a 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.

Summary:

I hope you will enjoy the tips while programming with the .NET Framework. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. We'll try to provide you Best .NET Training Course in our articles.

FAQs

It may be called any number of times, and thus the code therein should be safe in that case. The finalize method finishes releasing the remaining resources just before the object itself is freed from memory, and therefore it will only be called once

Destructors are explicitly defined and called by the programmer or the compiler, while finalizers are implicitly defined and called by the GC

The primary purpose of the Dispose method is to release any unmanaged resources held by an object. Because of this prevents resource leaks and ensures that resources are returned to the system promptly.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this