Wednesday, October 29, 2008

Finalize method

Finalize method allows an Object to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. This method acts as a safeguard to clean up resources in the event that the Dispose method is not called. Implement a Finalize method to clean up only unmanaged resources such as file handles or database connections. No need to implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.

The scope of the Object.Finalize method is protected. Every implementation of Finalize in a derived type must call its base type's implementation of Finalize. Finalize method is automatically called after an object becomes inaccessible, unless the object is re-registered using ReRegisterForFinalize and exempted from finalization by a call to SuppressFinalize.

Limitations:
1. The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose
method.

2. The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already finalized when the finalizer of Object A starts.

3. The thread on which the finalizer is run is unspecified.
Destructors are the C# mechanism for performing cleanup operations.
In C#
// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~MyResource()
{
     Dispose(false);
}
In VB.Net
' This finalizer will run only if the Dispose method does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this class.
Protected Overrides Sub Finalize()
Dispose(False)
      MyBase.Finalize()
End Sub

1 comment:

Winjelin Angkasa said...

Nikmati Bonus Menarik Dari Bolavita Sekarang...
-Nikmati Bous New member 10%
-Nikmati Bonus Cashback Hingga 10%
-Nikmati Juga Bonus jackpot Hingga Ratusan juta Rupiah Setiap harinya...

Info Lengkap Hubungi:
WA : 0812-2222-995
Line : cs_bolavita
Link : www.bolavita1.com

TERIMA KASIH