The dispose() function is used by an object when it needs to release any unmanaged resources it has been holding.
Despite having the same purpose as discard, finalise() does not ensure that an object will be removed from the trash.
Dispose() often works better because it has deterministic behaviour.
A type's Dispose method should release all of the resources that it is in possession of. It should release any resources that are a part of its base sorts by utilising the parent type's Dispose method. The parent type's dispose method, which should release any resources it holds and then call the parent type's dispose method, should transmit this pattern down the hierarchy of base types. To ensure that resources are always correctly cleaned up, a Dispose method should be callable several times without raising an exception.
A dispose method should invoke the GC.SuppressFinalize function for the object that is being disposed of. The GC.SuppressFinalize flag prevents the Finalise method from being executed if the object is already in the finalisation queue. Remember that using the Finalise approach degrades performance. If your Dispose method has already finished the required cleanup, the garbage collector won't need to invoke the object's Finalise function.
A Finalise method shouldn't throw exceptions because they cannot be handled by the programme and may cause the application to be terminated.
The garbage collector keeps track of objects that have Finalise methods using an internal structure known as the finalisation queue. Every time your application creates an object with a Finalise function, the garbage collector adds a new entry to the finalisation queue.
Comments
Post a Comment