Explain the purpose of Dispose method.Dispose method is used to free unmanaged resources like files, database connections etc.
It is called by the user code explicitly and the class implementing dispose method, must has to implement IDisposable interface.
.Net also offers Finalize method to clean up unmanaged resources held by an object before that object is destroyed. Unlike Dispose method, the Finalize method can't be called by user code but it is called by GC.
There is no performance cost associated with Dispose method whereas Finalize method has performance issue as it doesn't clean up memory immediately and is called by GC automatically.
It is always recommended not to implement a Finalize method for managed objects as GC cleans up managed resources automatically.
|