Constructor Execution Order in C#

Constructor Execution Order in C#

constructor-execution-order
You all will be familiar with the term constructor. In fact, it is one of the most favorite questions of the interviewer. I have seen many interviewees saying that the base class constructors are called first, but this is not the complete truth. Today I am going to clarify the answer to this question here.

Today's Agenda

Q01. What is the order precedence of the constructor execution in C#? Q02. What is the constructor execution order in C#? Q03. What is the constructor execution sequence in the case of static constructors? Q04. What is the constructor execution order in the C# class? Q05. What is the constructor execution order in the C# structure? Q06. What is the constructor execution order in inheritance?

Introduction

A constructor is nothing but a special type of method without any return type which has the same name as the class or struct name. We can also have multiple constructors in a class. Constructors are called automatically when we create the object of the class. One more important point is that constructors are always executed before any method or property. Constructors are used to initialize the data members or fields of the class or structure while creating the object of the class or structure. One of the common use of a constructor is to initialize the read-only fields in C# because read-only fields can only be initialized either at the time of declaration or in the constructor. Constructors are also used in dependency injection. You can read more about constructors here. Before going deep dive into the discussion, let's understand some basic terminology first. You can also watch the complete video of the constructor execution order here.
Default Constructor: A constructor is mandatory for each and every class in C#, so when we create a class without any constructor then, the compiler will automatically create a default constructor for those classes. Default constructors are parameterless constructors. A structure cannot have an explicit default instance constructor but it can have a static constructor that is always parameterless. You can watch more about the default constructor here.
Instance Constructor: An instance constructor is a member that implements the actions required to initialize an instance of a class. The instance constructor is a user-defined non-static constructor. It can be either parameterless or parameterized. We can have multiple instance constructors in a class. A constructor is invoked when you use the "new" operator, or use the various methods of reflection to create an instance of a class. You can watch more about instance constructors here.
Static Constructor: Static constructors are parameterless non-instance constructors. They are used to initialize a static class or static data members of a non-static class. Static constructors are invoked only once while creating the object of the non-static class or creating the first reference to a static member. Please read more about static classes and constructors. You can also watch about static constructors here.

CASE A: Constructor in Standalone Class

SCENARIO 1: Simple Class Without Static Constructor In a simple standalone instance class the default constructor is called if we are not passing any parameter. If we pass parameters, then the respective parameterized constructor is invoked.

simple-class
SCENARIO 2: Simple Class With Static Constructor A class can have only one static constructor. Static constructors are invoked only once while creating the object of the non-static class or creating the first reference to the static member. However, if the class has both instance and static constructors, the static constructor is always invoked first. You can read more about Static Class and Static Constructors here.


simpleclass-with-static-constructor


CASE B: Constructor in Inheritance

Constructors are never inherited, but the base class instance constructors are always called before any of the child class instance constructors. On more fact, you should know that a structure does not support inheritance, but it can implement interfaces. So a structure cannot inherit from any structure or class.
SCENARIO 1: Inheritance Without Static Constructor In case of inheritance if we create an object of the child class then the parent class instance constructor will be called before the child class constructor. This means the constructor calling order will be top to bottom in the inheritance hierarchy. So in this case the base class instance constructor will always invoked first followed by the derived class instance constructor.


inheritance
SCENARIO 2: Inheritance With Static Constructor In the case of a static constructor, the constructor execution order will be reversed order from the non-static constructor. This means it will be called from bottom to top . So in this case the derived class static constructor will always invoked first followed by the base class static constructor.


inheritance-with-static-constructor
SCENARIO 3: Inheritance With both Static and Instance Constructor This is a hybrid of previous both scenarios. So in this case the derived class static constructor will always invoked first followed by the base class static constructor. Then the base class instance constructor will be invoked followed by the derived class instance constructor.
inheritance-with-staticandinstance


CASE C: Constructor in Structure

Structures are used to represent a record or data structure that can contain both data members and function members. A structure or struct is a simple user-defined type, a lightweight alternative to a class. In C#, unlike class, a struct is the value type. A structure can contain only a parameterized constructor or a static constructor. A structure cannot have an explicit default instance constructor but it can have a static constructor that is always parameterless. A struct cannot contain any explicit default constructor i.e. parameterless constructor because every struct implicitly has a parameterless instance constructor that always sets the default value to its member fields. Until C# 10, the default constructor was not allowed in C# structures. But with C# 10, you can declare an instance parameterless constructor in a structure type and initialize an instance field or property at its declaration. C# 10 is supported on .NET 6 . You can read more about structure here.
SCENARIO 1: Structure Without Static Constructor In this case since until C#10, you are not allowed to create a default constructor in the structure, so if you create an instance of the structure without any constructor, the system defined implicit default constructor is always invoked, otherwise the respective constructor matching with the parameters.


structure
SCENARIO 2: Structure With Instance Constructor and Static Constructor If you create an instance of the structure with a parameterized instance constructor then the static constructor, will be invoked first followed by the instance constructor.


struct-with-static-const
SCENARIO 3: Structure With Default Constructor and Static Constructor This scenario is very interesting. If you create an instance of the structure with a default constructor then the static constructor will never be invoked at all. Here you can see that the rule of static constructor is compromised. This was the main issue until C# 9.


struct-default-static-const
SCENARIO 4: C#10 Structure With Default Constructor and Static Constructor As we know with C# 10 we can define explicit default constructor in structure as well. So the static constructor will be invoked first followed by the default instance constructor.


struct-csharp-10

Wrap Up

CRACK POINT 1: Static Constructors are always invoked first before any of the instance constructors whether it is in the stand-alone class or in the class inheritance. CRACK POINT 2: However, the instance constructor of the base class is always invoked first. CRACK POINT 3: The static constructor of the derived class is always invoked first before the static constructor of the base class. CRACK POINT 4: Until C# 9, in structure, the static constructor is never invoked with default constructor execution.

Recommended


Thanks for visiting this page. Please follow and join us on LinkedIn, Facebook, Twitter, Telegram, Quora, Instagram, WhatsApp, Tumblr, VK, Pinterest, and YouTube for regular updates.

Post a Comment

Please do not attach any link in the comment box.

Previous Post Next Post