Home »

Should I implement Finalize on my class? Should I implement IDisposable?

Question ListShould I implement Finalize on my class? Should I implement IDisposable?
jully882 author asked 8 years ago
1 Answers
ethanbrown author answered 8 years ago

This issue is a little more complex than it first appears. There are really two categories of class that require deterministic destruction – the first category manipulate unmanaged types directly, whereas the second category manipulate managed types that require deterministic destruction. An example of the first category is a class with an IntPtr member representing an OS file handle. An example of the second category is a class with a System.IO.FileStream member.
For the first category, it makes sense to implement IDisposable and override Finalize. This allows the object user to ‘do the right thing’ by calling Dispose, but also provides a fallback of freeing the unmanaged resource in the Finalizer, should the calling code fail in its duty. However this logic does not apply to the second category of class, with only managed resources.

In this case implementing Finalize is pointless, as managed member objects cannot be accessed in the Finalizer. This is because there is no guarantee about the ordering of Finalizer execution. So only the Dispose method should be implemented. (If you think about it, it doesn’t really make sense to call Dispose on member objects from a Finalizer anyway, as the member object’s Finalizer will do the required cleanup.)

For classes that need to implement IDisposable and override Finalize, see Microsoft’s documented pattern.Note that some developers argue that implementing a Finalizer is always a bad idea, as it hides a bug in your code (i.e. the lack of a Dispose call). A less radical approach is to implement Finalize but include a Debug.Assert at the start, thus signalling the problem in developer builds but allowing the cleanup to occur in release builds.

Please login or Register to Submit Answer