Dispose Vs Finalize Dot.Net

     

The difference between the Dispose and Finalize method in .NET are very suttle. Both methods deal with garbage collection (GC). The .NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources such as: 1. Files 2. Database connections

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

comments powered by Disqus