Information Technology Reference
In-Depth Information
Finalizers
Finalizers perform actions required to clean up or release unmanaged resources before an
instance of a class is destroyed. The important things to know about finalizers are the
following:
￿
You can only have a single finalizer per class.
￿
A finalizer cannot have parameters.
￿
A finalizer cannot have accessibility modifiers.
￿
A finalizer has the same name as the class, but is preceded by a tilde character (pro-
nounced TIL-duh ).
￿
A finalizer only acts on instances of classes. Hence, there are no static finalizers.
￿
A finalizer cannot be called explicitly by your code. It is called in the garbage collection
process, when your class is no longer accessible.
For example, the following code illustrates the syntax for a finalizer of a class called Class1 :
Class1
{
~Class1() // The finalizer
{
CleanupCode
}
...
}
Some important guidelines for using finalizers are the following:
￿
Don't implement a finalizer if you don't need one. They can incur performance costs.
￿
A finalizer should only release external resources that the object owns. It should not
access other objects.
Since it cannot be called explicitly, make it less visible by not declaring it with public
access.
￿
Search WWH ::




Custom Search