In this article I am going to discuss about types of constructors in C# with proper examples but before that if you had not read my previous article of C# constructor then must read it for more clear understanding. As in previous article we had already discussed what is constructor and what is the use of constructor so now its time to know about the various types of constructors in c#.

C# Types of Constructors

In C#, there are mainly five types of constructors are there which is having own purpose and use cases. The most common one is default constructor which is created automatically when we create instance of class. The following are the types of constructors in csharp.

csharp-types-of-constructors

1. Default Constructor

The default Constructor is one which does not contain any parameters. This constructor will created automatically when we create instance of class or also created explicitly by programmer for initializing variables. If we not create any constructor in our class then a default constructor will created by the compiler. The default constructor initializes all integer fields with zero and string with null. The default constructor is also known as parameterless constructor.

C# Program to Explain Default Constructor

using System;
namespace DefaultConstructor
{
    class Student
    {
        public int Age;
        public bool IsRight;
    }
    
    class Test
    {
        static void Main(string[] args)
        {
            Student obj = new Student();
            Console.WriteLine("Student Age is:  " + obj.Age);
            Console.WriteLine("Student Age is:  " + obj.IsRight);
            Console.ReadKey();
        }
    }
}
Output:
Student Age is:  0
Student Age is:  False

In above code you can see I have not created any constructor but still am able to use the field values of class student and you can also see the default values of integer and boolean which is initialized by the default constructor.

2. Parametrized Constructor

The constructor with any number of parameter is called parametrized constructor. In parametrized constructor we can pass any number of parameters. In C#, this is one of the mostly used type of constructor.

C# Program to Explain Parameterized Constructor

using System;
namespace ParameterizedConstructorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Test obj = new Test(10);
            Console.ReadKey();
        }
    }

    public class Test
    {
        public Test(int i)
        {
            Console.WriteLine("The Square root of i is : "+i*i);
        }
    }
}
Output:
The Square root of i is : 100

3. Copy Constructor

This constructor is used to copy the value of one object from another object. And, if you also want to create the multiple instances with the same value then there you can use the copy constructor.

C# Program to Explain Copy Constructor

using System;
namespace CopyConstructorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Test obj = new Test(100);
            obj.Display();
            
            Test obj2 = new Test(obj);
            obj2.Display();
            
            Console.ReadKey();
        }
    }

    public class Test
    {
        int x;

        public Test(int i)
        {
            x = i;
        }

        //Copy Constructor
        public Test(Test obj)
        {
            x = obj.x;
        }

        public void Display()
        {
            Console.WriteLine("Value of X = "+x);
        }
    }
}
Output:
Value of X = 100
Value of X = 100

In above program firstly we have created one parameterized constructor then again created one parameterized constructor which takes the object of constructor’s class type therefore it is also known as copy constructor. For e.g if the class name is test then the object of copy constructor is also of type test. Then in the main method we have created one object of class test which takes value of 100 as parameter. After passing the value again we have created another object of class test with name of obj2 but this time we have passed the first object as parameter to second object. Thus you can see we have two instances of same class with same value.

4. Private Constructor

The constructor with private keyword is known as private constructor. If the constructor is private then you cannot create the instance outside of the class. Thus you can only create the instance of private constructor inside the same class only. But if you want to access the properties of another class into your class then you have to declare the static fields and functions.

using System;
namespace privateConstructorDemo {

class Test {

	private Test()
	{
	}

	public static int count;

	public static int testCount()
	{
           count = count * count;
           return count;
	}
}

public class Program {
	public static void Main()
	{
		Test.count = 19;
		Test.testCount();
		Console.WriteLine(Test.count);

		Test.testCount();
		Console.WriteLine(Test.count);
	}
}
}
Output:
361
130321

Points To Remember about C# Private Constructor:

  1. It is the implementation of a singleton class pattern.
  2. We need to use the private constructor in C# when the class contains only static members.
  3. Using a private constructor is not possible to create an instance from outside the class.

5. Static Constructor

In C#, we can also declare static constructor with static keyword. Static Constructor does not take any access modifiers like public, private and protected. A static constructor is initialized static fields or data of the class and to be executed only once. Static constructor called only once during the creation of first instance of class.

using System;
namespace StaticConstructorDemo
{
    public class Test
    {
        static Test()
        {
            Console.WriteLine("Static Constructor Executed!");
        }
        
        public Test(int i)
        {
            Console.WriteLine(i);
        }
        static void Main(string[] args)
        {
            //Here we does not call static constructor but it will call automatically when the instance of class is created.
            Test obj=new Test(1);
            Console.WriteLine("Main Method Executes after static constructor");
            Console.ReadKey();
        }
    }
}
Output:
Static Constructor Executed!
1
Main Method Executes after static constructor

Points To Remember :  

  • It can’t be called directly.
  • When it is executing then the user has no control.
  • It does not take access modifiers or any parameters.

In above program you can see that I have created static constructor and parameterized constructor but am calling only parameterized constructor but still static constructor invoked because static constructor is invoked automatically when the instance of any class is invoked.

Conclusion

In this C# tutorials article I had covered all the types of constructors with proper examples. Hope you understand all the types of constructors in C# and like this article. This thing may be not understand to you with doing once only but as you work with all the types of constructors of c# then you get to know the use case of each constructors. In my next article I am going to discuss about access specifiers in csharp programming.