For some reason FXCop seems to think I should be calling GC.SuppressFinalize in Dispose, regardless of whether I have a finalizer or not.
Am I missing something? Is there a reason to call GC.SuppressFinalize on objects that have no finalizer defined?
Dispose should call GC. SuppressFinalize so the garbage collector doesn't call the finalizer of the object. To prevent derived types with finalizers from having to reimplement IDisposable and to call it, unsealed types without finalizers should still call GC.
Finalize method, is used to release unmanaged resources before an object is garbage-collected. If obj does not have a finalizer or the GC has already signaled the finalizer thread to run the finalizer, the call to the SuppressFinalize method has no effect.
Finalizers (historically referred to as destructors) are used to perform any necessary final clean-up when a class instance is being collected by the garbage collector. In most cases, you can avoid writing a finalizer by using the System. Runtime.
SuppressFinalize tells the GC that the object was cleaned up properly and doesn't need to go onto the finalizer queue. It looks like a C++ destructor, but doesn't act anything like one. The SuppressFinalize optimization is not trivial, as your objects can live a long time waiting on the finalizer queue.
There's no need to call GC.SuppressFinalize(this)
in Dispose, unless:
Object
, then the object is not considered to need finalizing and isn't put on the finalization list upon GCI would say, assuming you don't have any of the above cases, that you can safely ignore that message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With