Constructors in C# initialize the members of the class to which they belong and design your classes with elegance Constructors are member functions or methods of a class that have the same name as the class name and are used to initialize the members of a class. Such methods are invoked when an instance of the class is created. Note that a struct can also have a constructor. However, unlike a class, you cannot have an explicit parameter-less constructor in a struct Here’s the syntax you should follow to define a constructor for a class. [modifier] name (parameters) { // usual code } The C# programming language provides support for the following modifiers when you create a constructor for a class. public protected internal private extern A few points to note A constructor cannot be virtual and cannot return any value. OK, but what is a virtual method anyway? Virtual methods are used to implement late binding. You can declare a method as virtual by specifying the keyword “virtual” in the method signature. A virtual method allow subclasses of the type to override the method. A constructor of a class cannot be virtual while a destructor can be virtual. A constructor cannot be virtual primarily because the virtual table or vtable is not available in the memory when it is in execution. Default constructor A class provides a constructor be default — such a constructor is known as the default constructor. A default constructor for a class is invoked when the class is instantiated using the “new” operator but no argument is passed to the constructor while constructing the instance. Constructors are called in the order of inheritance. In contrast, the destructors are called in the reverse order of inheritance. You can have overloaded constructors but not overridden constructors as constructors are not inherited. Static constructor The static keyword in the C# programming language allows you to define static classes and static members. A static object is one that persists in the memory till the time the application is in execution. Note that you can have a static constructor in either a static class or a non-static class. A static constructor in a class is used to initialize the static members of a class. The static constructor of a class is invoked the first time a static member of the class is assessed. A static constructor can only access static members of a class. Note that a class can have one and only one static constructor, i.e., you cannot have overloaded static constructors for a class. A static constructor cannot accept any parameter and cannot have any access modifiers. Also, a static constructor is executed just once in an application domain — it is called when a static member of the class is accessed. You don’t need to create an instance of a class to invoke its static constructor. Instance constructor A non-static or an instance constructor is one that is executed when an instance of the class to which it belongs, is created. Instance constructors can have public, private, protected, external, or internal modifiers. Instance or non-static constructors of a class can be overloaded. Constructors can also be chained, i.e., you can invoke a base class constructor from a derived class constructor. In essence, a constructor of a derived class can call the constructor of its base using a method known as constructor chaining. Note that for obvious reasons, constructor chaining is supported only by the non-static constructors of a class. Non-static constructors can also be private. Such constructors when present in a class prevent the class from being inherited. The following code snippet illustrates how you can have overloaded constructors in a class. public class Employee { //Private members public Employee() { //This is the default constructor } public Employee(int employeeId) { //This is a one argument constructor. } public Employee(int employeeId, int departmentId) { //This is a two argument constructor. } } Let’s now dig into some code. Consider the following class that contains a static and also an instance constructor. class StaticClassDemo { static StaticClassDemo() { Console.WriteLine ("This is a static constructor."); } public StaticClassDemo() { Console.WriteLine ("This is an instance constructor."); } } You can now create an instance of this class using the code snippet shown below. static void Main(string[] args) { StaticClassDemo obj = new StaticClassDemo(); Console.ReadLine(); } When this code is executed, you would observe that the static constructor is executed first, followed by the non-static constructor. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos