Total Page Preview:   000000018000

What is Difference Between Finalize and Dispose Method

 

Dot 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.


In Hindi

" Jo resources managed nahi hote hai like files, database connections, COM etc. unko release karne ke liye Dot framework two methods Finalize and Dispose provide karta hai. Yah article aapki help karega Finalize and Dispose method ke difference ko samjhane me"

Difference Between Finalize and Dispose Method

  Dispose Finalize() Method
1

It is used to free unmanaged resources like files, database connections etc. at any time.


In Hindi

"Yah unmanaged resources like files, database connections etc. ko kisi bhi samay free karne ke liye use hota hai"

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.


In Hindi

"Yah unmanaged resources (jab aap implement karte ho) like files, database connections etc. ko ek object ke dwara aur ush object ke destroy hone se pahle free karne ke liye use hota hai"

2

Dispose() belongs to the IDisposable interface.


In Hindi

"Dispose() IDisposable interface se belong karta hai"

Finalize() belongs to the Object class.


In Hindi

"Finalize() Object class se belong karta hai"

3

Explicitly, it is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.


In Hindi

"Explicitly, yah user code ke dwara call kiya jata hai aur jo class dispose method ko implement karti hai, ushe IDisposable interface ko bhi implement karna padta hai"

Internally, it is called by Garbage Collector and cannot be called by user code.


In Hindi

"Internally, yah Garbage Collector ke dwara call kiya jata hai aur ishe user code ke dwara call nahi kar sakte"

4

It's implemented by implementing IDisposable interface Dispose() method.


In Hindi

"Iska implementation IDisposable interface Dispose() method ke implementation ke dwara hota hai"

It's implemented with the help of destructor in C++ & C#.


In Hindi

"C++ & C# me iska implementation destructor ki help se hota hai"

5

There is no performance costs associated with Dispose method.

In Hindi

"Dispose method ke sath koi performance cost associate nahi hoti hai"

There is performance costs associated with Finalize method since it doesn't clean the memory immediately and called by GC automatically.


In Hindi

"Finalize method ke sath performance cost associate hoti hai jab tak yah memory ko immediately clean nahi kar deti aur GC ke dwara automatically call nahi ho jati"

Dispose() Method Example:

//Implement Dispose Method.
public class TestDispose : IDisposable
{
private bool disposed = false ;
//Implement IDisposable.
public void Dispose()
{
Dispose( true);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// clean unmanged objects
}
// clean unmanaged objects).
disposed = true;
}
}
}

Finalize() Method Example:

// Implementing Finalize method
public class Sample
{
public Sample() {
//constructor will create.
}
//At runtime destructor automatically Converted to Finalize method.
~Sample()
{
// your clean up code
}
}
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.
 
 

 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments


                           
                           

                           

                           

Facebook User: