Constructors in C#

This article explains about Constructors, types of Constructors and why we need Constructors in a class. Every class requires a constructor to create a instance of that class.

It is a special method present in the class. To initialize the variables of the class, we need Constructor.

The name of the constructor should be same as the name of the class.

Constructor is a void type, it will not return anything. Constructor is mainly used to create the instance of the class.

The responsibility of the constructors is to initialize the variables of the class.

There are two ways that Constructors can be defined.

Explicit Constructors

Explicit constructors are created or defined by programmers. They manually define the constructor.

Class Student
{
    string name;
    int age;
    public Student(string Name, int Age)
    {
       name = Name;
       age  = Age;
    }
}

An explicit constructors can also be parameterized or parameterless constructors.

Implicit Constructors

Implicit Constructors are created by Compiler. When a developer fails to create a constructor, on behalf of the developer an implicit constructor gets defined in that class by compiler. Consider the below example, when a programmer runs this code,

Class Student
{
   int age;
   string str;
   bool flag;
}

It gets compiled and becomes like below. All the variables are initialized with default values.

Class Student
{
    int age;
    string str;
    bool flag;
    public Student()
    {            
       age = 0;
       str = null;
      flag = false;
    }
}

Initialization is done for each and every variable of a class implicitly by compiler. Implicit constructors are also called as Default constructors or parameterless constructors.

By default, Implicit constructors are always public.

When an instance of class is created, a call to the constructor is made. Then the compiler checks for explicit constructor, when it identifies the explicit constructor, it executes that code. Otherwise it creates a new implicit constructor, initialize the variables and executes them.

Types of Constructors

There are four types are constructors in C#. They are:

  1. Default or Parameterless Constructors
  2. Parameterized Constructors
  3. Copy Constructor
  4. Static Constructor

Default or Parameterless constructors

When a constructor doesn’t take any parameters, then we call it as a default or parameterless constructors. These constructors can be defined either by programmer explicitly or by the compiler implicitly. When a programmer fails to define the constructor on behalf of the programmer compiler generates a constructor called implicit constructor.

Parameterized constructors

When a constructor is defined with any parameters, then it is called as a parameterized constructors. These constructors can be defined by the programmers only. It never can be defined implicitly.

Copy Constructors

When we want to create a multiple instances of the same value, then we use these Copy constructor. In a copy constructor, constructor takes same class as a parameter to it.

class CopyConstructor
{
    int a;
    public CopyConstructor(CopyConstructor obj)
    {
        a = obj.a;
    }
    public void Display()
    {
        Console.WriteLine("Value of a is " + a);
    }
    public static void Main(string[] args)
    {
        CopyConstructor cc1 = new CopyConstructor(10);
        cc1.Display();

        CopyConstructor cc2 = new CopyConstructor(cc1);
        cc2.Display();

        Console.ReadLine();
    }
}

Copy constructor importance:

Consider this example, I have created one class with 10 values, now I want to declare one more instance of class with the same 10 values. Instead of giving manually, I can reuse the first instance values by passing the object of first class as a parameter to the constructor.

For this copy constructor, we need to pass the same class as parameter to the constructor.

public CopyConstructor(CopyConstructor obj)
{
     a = obj.a;
}

All these constructors we have defined till now are non-static or instance constructors.

Static Constructor

When a constructor is declared with a static modifier, then it is said to be a static constructor.

class Student
{
  //Static constructor  
  static Student() 
  {
  }

//Implicit default constructor
  public Student()
  {
  }
}

When a class contains any static variables, then only implicit static constructor will be present or else we need to define them explicitly.

Static constructors are first to execute under any class. Static constructors are responsible to initialize the static variables. These constructors are never be called explicitly.

Static constructors can’t be parameterized so overloading static constructor is not possible. Because static constructors are first to execute under any class and it is implicitly called, so we can’t parameterize them.

Since it is implicitly called, we never get a chance to pass the parameter in it.

class Student
{ 
  static Student() 
  {
     Console.WriteLine("Static constructors are the first block to execute under any class. They are implicitly called, it never be called explicitly. To initialize the static variables, static constructor will be called implicitly");
  }

  static void Main()
  {
  }  
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s