Finalizers on derived classes

suggest change

Finalizers 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();

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents