.net Dispose Vs Finalize

The Dispose Finalize pattern is being used to free the resources in .net. In the C++ world, the resource freeing was deterministic. In .net, it is not. The rule of thumb is to use Dispose to free non-managed resources and Finalize to free managed resources. To use Dispose method, the class has to implement the IDisposable interface. The Dispose method needs to be called explicitly. Normally it is done in using statement. The Finalize is defined like a C++ destructor with ~. The Finalize is expensive and run from different threads by the .net. If Dispose is being called to free a resource, make sure there is a suppress call to Finalize to supress further resource freeing.

Leave a Reply

You must be logged in to post a comment.