#428 – A Finalizer Should Always Call the Finalizer of Its Base Class

An implementation of Object.Finalize for a particular class in .NET should always call the Finalize method of its base class.  In the case of C#, you can’t explicitly override Finalize, but you provide a finalizer using the destructor (~) syntax.  When you do this, the compiler will automatically add code to ensure that your destructor calls Finalize in the base class.

In the example below, I write a simple destructor for the Dog class.  We can see in the IL that the Finalize method has been overridden and that it calls System.Object.Finalize in the finally clause.

        ~Dog()
        {
            Trace.WriteLine("This dog is on the way out..");
        }

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #428 – A Finalizer Should Always Call the Finalizer of Its Base Class

  1. Pingback: #832 – The Sequence in Which Finalizers Are Called | 2,000 Things You Should Know About C#

Leave a comment