Finalizers on derived classes
suggest changeFinalizers on one object run from the most derived type toward the base, the reverse of construction order. Finalizer order among distinct objects is not defined. The following code demonstrates the derived-then-base order:
class TheBaseClass
{
~TheBaseClass()
{
Console.WriteLine("Base class finalized!");
}
}
class TheDerivedClass : TheBaseClass
{
~TheDerivedClass()
{
Console.WriteLine("Derived class finalized!");
}
}
//Don't assign to a variable
//to make the object unreachable
new TheDerivedClass();
// Just to make the example work;
// this is otherwise NOT recommended!
GC.Collect();
GC.WaitForPendingFinalizers();
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents