Pages

Thursday 29 May 2014

Difference between Finalize & Dispose Method

Finalize
Dispose
Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed.
It is used to free unmanaged resources like files, database connections, COM etc. at any time.
Internally, it is called by Garbage Collector and cannot be called by user code.
Explicitly, it is called by user code and the class implementing dispose method must implement IDisposable interface.
It belongs to Object class.
It belongs to IDisposable interface.
Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.
Implement this when you are writing a custom class that will be used by other users.
There is performance costs associated with Finalize method.
There is no performance costs associated with Dispose method.