What is difference between
dispose () and
finalize () method in .net?
Dispose () and
finalize () methods are used to release memory held by unmanaged resources database connections, file IO operations, COM objects. The main difference in between two is that finalize () method is
called by garbage collector (GC) internally to free up memory while dispose () method is
called by user code explicitly.
To call dispose () method explicitly, class need to implement IDisposible interface.
It is best practice to implement dispose () method to free up memory as it will immediately free up memory as soon as the currently executing code block completes. Garbage collector does not call
finalize () method immediately, so it has some