According to Essential C# 6.0 you should:
AVOID calling Dispose() on owned objects that have a finalizer. Instead, rely on the finalization queue to clean up the instance.
- Could someone please elaborate on this as I'm not clear on what the point of Dispose is if we're not to call it from owned objects?
- Apart from Reflection, how would you figure out if the object has a
Finalizer
? - How do you figure out when to call
Close()
/Close()
+Dispose()
other than searching the API documentation (if you have access to it and it exists) or Reflection? I see a lot of questions around the net for very specific types (MemoryStream
/Form
/SqlConnection
/ etc ) but I'm looking more at "how to figure it out yourself".
According to the Dispose Pattern you should:
CONSIDER providing method Close(), in addition to the Dispose(), if close is standard terminology in the area. When doing so, it is important that you make the Close implementation identical to Dispose and consider implementing the IDisposable.Dispose method explicitly.
but there are times when you should call both like with Form
, etc. Questions like "Close and Dispose - which to call?" get close but there's no defined approach apart from
As usual the answer is: it depends. Different classes implement IDisposable in different ways, and it's up to you to do the necessary research.
Edit: Here is the full guideline, I haven't asked for permission to reproduce but since it's a guideline (thereby assuming it's supposed to be freely shared public knowledge) and not some part of the actual training material, I'm hoping I'm not breaking any rules.
Guidelines
DO implement a finalizer method only on objects with resources that are scarce or expensive, even though finalization delays garbage collection.
DO implement IDisposable to support deterministic finalization on classes with finalizers.
DO implement a finalizer method on classes that implement IDisposable in case Dispose() is not invoked explicitly.
DO refactor a finalization method to call the same code as IDisposable, perhaps simply calling the Dispose() method.
DO NOT throw exceptions from finalizer methods.
DO call System.GC.SuppressFinalize() from Dispose() to avoid repeating resource cleanup and delaying garbage collection on an object.
DO ensure that Dispose() is idempotent (it should be possible to call Dispose() multiple times).
DO keep Dispose() simple, focusing on resource cleanup required by finalization.
AVOID calling Dispose () on owned objects that have a finalizer. Instead, rely on the finalization queue to clean up the instance.
AVOID referencing other objects that are not being finalized during finalization.
DO invoke a base class’s Dispose() method when overriding Dispose().
CONSIDER ensuring that an object becomes unusable after Dispose() is called. After an object has been disposed, methods other than Dispose() (which could potentially be called multiple times) should throw an ObjectDisposedException.
DO implement IDisposable on types that own disposable fields (or properties) and dispose of said instances.