Yes, we can call
dispose
method in
Finalize()
.
By doing this, we force the
dispose
method to be called automatically.
We can call the
dispose
method in
Finalize()
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 trips.
Public Class ClsTesting
Implements IDisposable
Public Overloads Sub Dispose()Implements IDisposable.Dispose
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose()
End Sub
End Class
Hope this may help you. :)