top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between Dispose and Finalize method?

+1 vote
358 views
What is difference between Dispose and Finalize method?
posted Mar 22, 2017 by Madhavi Kumari

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

.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

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

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.

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

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.

answer Mar 22, 2017 by Manikandan J
...