using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Destructor
{
class First
{
~First()
{
System.Diagnostics.Trace.WriteLine(@"'First' Destructor is called");
}
}
class Second:First
{
~Second()
{
System.Diagnostics.Trace.WriteLine(@"'Second' Destructor is called");
}
}
class Third:Second
{
~Third()
{
System.Diagnostics.Trace.WriteLine(@"'Third' Destructor is called");
}
}
class Program
{
static void Main(string[] args)
{
//SEE TRACE LOGS FROM 'OUTPUT' WINDOW AFTER CLOSING THE PROGRAM
//Destructor are called and followed in order of Inheritance Chain MostDerived to LeastDerived (that means Third>Second>First)
new Third(); //Here we are creating an instance of the most derived class (Third)
Console.WriteLine("Press any key");
Console.Read();
}
}
}
/* Output (to VS Output Window):
Third's destructor is called.
Second's destructor is called.
First's destructor is called.
*/