Difference between Finalize() and Dispose()

finalize-dispose

Finalize()

Finalize() is called by the Garbage Collector before an object that is eligible for collection is reclaimed. Garbage collector will take the responsibility to deallocate the memory for the unreferenced object. The Garbage Collector calls this method at some point after there are no longer valid references to that object in memory.

The framework does not guarantee that when this will happen, you can force for Garbage Collection but it will hurt performance of your program. Finalize() belongs to the Object class and it will be called by the runtime.

Dispose()

There are some resources like windows handles, database connections , network connections, files, etc. which cannot be collected by the Garbage Collector. If you want to explicitly release some specific objects then this is the best to implement IDisposable and override the Dispose() method of IDisposable interface.

The Dispose() method is not called automatically and you must explicitly call it from a client application when an object is no longer needed. Dispose() can be called even if other references to the object are alive.

It is recommends that you implement both Dispose() and Finalize() when working with unmanaged Objects.