Explanation of code: Finalize() is called recursively for all instances in the inheritance chain, from most derived to least derived.

Garbage collector

Garbage collector checks for objects that are no longer being used by application, if it treated an object eligible for destruction, it calls the destruction and reclaims the memory used to store the object.

GC keeps tracks of all the objects and ensures that each object gets destroyed once.

GC ensures that objects, which are being referenced, are not destroyed.

GC destroys the objects only when necessary. Some situations of necessity are memory is exhausted or user explicitly calls System.GC.Collect()method.

Note : Execution order: Base
constructor is getting called first. In general, destructors are called in the
reverse order of the constructor calls.

Note : Execution order base constructor is getting called first. In general, destructors are called in
the reverse order of the constructor calls.

Some Useful Points

When your application encapsulates unmanaged resources such as:

Windows

Files

Network connections

you should use destructors to free those resources.

When an object is eligible for destruction, the garbage collector runs the Finalize () method of that object.

Empty destructors should not be used

Reason - When a class contains a destructor , an entry is created in the finalize queue. if the destructor is empty , this just causes a needless loss of performance.

Explicit release of resources

Suppose an application uses a costly external resources, then a way to explicitly release the resource before the garbage collector frees the object.IDisposable Interface - Defines a method to release allocated resources.
Namespace : System
Assembly : mscorlib (in mscorlib.dll)