Showing posts with label Order of constructor execution. Show all posts
Showing posts with label Order of constructor execution. Show all posts

Tuesday 28 January 2020

Constructor execution hierarchy in C#

Class C1
{
  public C1()
   {
     Console.WriteLine("Normal C1");
   }
  static C1()
   {
     Console.WriteLine("Static C1");
   }
}
--------------------------------
Class C2: C1
{
  public C2()
   {
    Console.WriteLine("Normal C2");
   }
  static C2()
   {
     Console.WriteLine("Static C2");
   }
}

/////////////////////////////
Output:

Static C2
Static C1
Normal C1
Normal C2