Constructors in C#

Constructors have a very special meaning to the compiler and CLR but sometimes its flow seems difficult for a developer. This article's explanation is simple but provides important insights into Constructors.

So, What is a Constructor?
 
A Constructor is a special method in a class/struct with the same name as the class/struct without any return type that initializes fields and members of a class/struct.

A constructor can only be called by:
  • The Compiler using the New keyword to initialize a class/struct. 
  • Another constructor of the same class/struct using the this keyword.
  • Constructors of the derived class using the base() keyword.

Types of Constructors in C#

The following are the types of Constructors in C#:

  • Default constructor.
  • Parameterized Constructor.
  • Instance Constructor.
  • Static Constructor.
  • Private Constructor. 
Default Constructors

The following describes Default Constructors:

  1. A Constructor with no parameter is called a Default Constructor.
  2. A Default Constructor is called by the compiler when no arguments are passed to the New operator while creating an object of a class or struct.
  3. If there is no constructor in a non-static class then a Public Default Constructor is provided by the compiler so that a class can be instantiated.
  4. A struct cannot have an explicit Default Constructor (we cannot define an explicit Default Constructor in a struct), but it is always provided by the compiler to initialize each field of the struct to its default value.

Parameterized Constructors

The following describes Parameterized Constructors:

  1. A constructor with parameters is called a parameterized Constructor.
  2. A class or struct can have multiple parameterized constructors as long as they have a different method signature. They follow the same concept as method overloading.
  3. The compiler provides Default Constructors only if there is no constructor (default or parameterized) defined in a class.
  4. Parameterized Constructors can exist even without the existence of Default Constructors.

Static Constructors

The following describes Static Constructors:

  1. To initialize a Static Class or static variables in a non-static class, Static Constructors are used
  2. Access Modifiers are not allowed on Static Constructors
  3. Static Constructors cannot be parameterized
  4. There can be only one Static Constructor per class
  5. Static Constructors cannot have an explicit "this" or "base" constructor call, in other words Static Constructors cannot be called directly
  6. Static Constructors are called automatically before the first instance of a class is created or any static member is referenced
  7. Static Constructors are called only once in the lifetime of a class

Private Constructors

The following describes Private Constructors:

  1. A constructor becomes a private constructor when we declare it with the private access specifier.
  2. Private Constructors can neither be instantiated nor inherited from another class.
  3. An object of a class can only be created in the class itself.
  4. Microsoft recommends its use for the implementation of the Singleton Pattern.

Constructor Chaining

The following describes Constructor Chaining:

  1. A constructor can call another constructor of the same class or of the base class
  2. Since one constructor can invoke another, this sometimes can cause the execution of multiple constructors; that is referred to as Constructor Chaining
  3. If a class is not derived from any other class then the following would be the chain: 
    1. Static Constructor. 
    2. Instance Constructor
  4. If a class is derived from any other class then the following would be the chain:  
    1. Derived Static Constructor.  
    2. Base Static Constructor. 
    3. Base Instance Constructor.
    4. Derived Instance Constructor.

I hope all topics related to constructors have been covered. Please let me know if I have missed any or you need more explanations or examples.

!! Happy Programming !!

Up Next
    Ebook Download
    View all
    Learn
    View all