In C# what is the difference between a destructor and a Finalize method in a class?

Wikipedia has some good discussion on the difference between a finalizer and a destructor in the finalizer article.

C# really doesn’t have a “true” destructor. The syntax resembles a C++ destructor, but it really is a finalizer. You wrote it correctly in the first part of your example:

~ClassName() { }

The above is syntactic sugar for a Finalize function. It ensures that the finalizers in the base are guaranteed to run, but is otherwise identical to overriding the Finalize function. This means that when you write the destructor syntax, you’re really writing the finalizer.

According to Microsoft, the finalizer refers to the function that the garbage collector calls when it collects (Finalize), while the destructor is your bit of code that executes as a result (the syntactic sugar that becomes Finalize). They are so close to being the same thing that Microsoft should have never made the distinction.

Microsoft’s use of the C++’s “destructor” term is misleading, because in C++ it is executed on the same thread as soon as the object is deleted or popped off the stack, while in C# it is executed on a separate thread at another time.

Leave a Comment