NOTE:-How do I force the Dispose method to be called automatically, as clients can forget to call Dispose method?
Call the Dispose method in Finalize method and in Dispose method suppress the finalize method using GC.SuppressFinalize. Below is the sample code of the pattern. This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice.
It will suppress the finalize method thus avoiding the two trip.
Public Class ClsTesting
Public Overloads Sub Dispose()Implements IDisposable.Dispose
' write ytour clean up code here
Protected Overrides Sub Finalize()