Ever beat a dead horse?
Brent Rector of Wintellect discusses Finalizers, sharing how the GC process really behaves and why we should think twice thrice before using Finalizers. Make sure your design warrants the extra overhead.
Other highlights from Brent's article include:
- Despite some semantic parallels, C++ destructors behave very differently than C# destructors (and .Net destructors in general).
- Using Finalizers can cause a significant performance penalty, especially when one considers the multi-step process (and threads) involved with Finalization.
- If you must use a Finalizer, keep it as simple as possible.
- His opinion is that since Java has finalizers, MSFT added finalizers to .Net to match up in a feature-by-feature comparison with Java
- You can use the Dispose pattern if you want to achieve non-deterministic finalization (I discussed this here)
If you must use Finalizers in your design, keep them brief and free of exception-throwing code; use the Dispose() pattern and document your classes so clients know to invoke the Dispose() method appropriately (or with a using statement in c#).