Difference Between Finalize and Dispose Method

Difference Between Finalize and Dispose Method

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

.NET 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 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 9th time in a row (2016-2024). 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.
Accept cookies & close this