What is a constructor in C#?
A special method of the class that is automatically invoked when an instance of the class is created is called a constructor. The main use of constructors is to initialize the private fields of the class while creating an instance for the class. When you have not created a constructor in the class, the compiler will automatically create a default constructor of the class. The default constructor initializes all numeric fields in the class to zero and all string and object fields to null.
Some of the key points regarding constructor are
- A class can have any number of constructors.
- A constructor doesn't have any return type, not even void.
- A static constructor can not be a parametrized constructor.
- Within a class, you can create one static constructor only.
In C#, constructors can be divided into 5 types
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Static Constructor
- Private Constructor
Now, let's see each constructor type with the example below.
Default Constructor in C#
A constructor without any parameters is called a default constructor; in other words, this type of constructor does not take parameters. The drawback of a default constructor is that every instance of the class will be initialized to the same values and it is not possible to initialize each instance of the class with different values. The default constructor initializes:
- All numeric fields in the class to zero.
- All string and object fields to null.
Example
using System;
namespace DefaultConstractor
{
class addition
{
int a, b;
public addition() //default contructor
{
a = 100;
b = 175;
}
public static void Main()
{
addition obj = new addition(); //an object is created , constructor is called
Console.WriteLine(obj.a);
Console.WriteLine(obj.b);
Console.Read();
}
}
}Now run the application, the output will be as following:

Parameterized Constructor in C#
A constructor with at least one parameter is called a parameterized constructor. The advantage of a parameterized constructor is that you can initialize each instance of the class with a different value.
using System;
namespace Constructor
{
class paraconstrctor
{
public int a, b;
public paraconstrctor(int x, int y) // decalaring Paremetrized Constructor with ing x,y parameter
{
a = x;
b = y;
}
}
class MainClass
{
static void Main()
{
paraconstrctor v = new paraconstrctor(100, 175); // Creating object of Parameterized Constructor and ing values
Console.WriteLine("-----------parameterized constructor example by vithal wadje---------------");
Console.WriteLine("\t");
Console.WriteLine("value of a=" + v.a );
Console.WriteLine("value of b=" + v.b);
Console.Read();
}
}
}Now run the application, the output will be as following:
Copy Constructor in C#
The constructor which creates an object by copying variables from another object is called a copy constructor. The purpose of a copy constructor is to initialize a new instance to the values of an existing instance.
Syntax
public employee(employee emp)
{
name=emp.name;
age=emp.age;
}The copy constructor is invoked by instantiating an object of type employee and bypassing it the object to be copied. Example
employee emp1=new employee (emp2);Now, emp1 is a copy of emp2.
Let's see its practical implementation.
using System;
namespace copyConstractor
{
class employee
{
private string name;
private int age;
public employee(employee emp) // declaring Copy constructor.
{
name = emp.name;
age = emp.age;
}
public employee(string name, int age) // Instance constructor.
{
this.name = name;
this.age = age;
}
public string Details // Get deatils of employee
{
get
{
return " The age of " + name +" is "+ age.ToString();
}
}
}
class empdetail
{
static void Main()
{
employee emp1 = new employee("Vithal", 23); // Create a new employee object.
employee emp2 = new employee(emp1); // here is emp1 details is copied to emp2.
Console.WriteLine(emp2.Details);
Console.ReadLine();
}
}
}Now run the program, the output will be as follows:

Static Constructor in C#
When a constructor is created using a static keyword, it will be invoked only once for all of the instances of the class and it is invoked during the creation of the first instance of the class or the first reference to a static member in the class. A static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.
Some key points of a static constructor are:
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control over when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
Syntax
class employee
{
// Static constructor
static employee(){}
}Now let's see it practically.
using System;
namespace staticConstractor
{
public class employee
{
static employee() // Static constructor
declaration{Console.WriteLine("The static constructor ");
}
public static void Salary()
{
Console.WriteLine();
Console.WriteLine("The Salary method");
}
}
class details
{
static void Main()
{
Console.WriteLine("----------Static constrctor example by vithal wadje------------------");
Console.WriteLine();
employee.Salary();
Console.ReadLine();
}
}
}Now run the program the output will look as in the following:

Private Constructor in C#
When a constructor is created with a private specifier, it is not possible for other classes to derive from this class, neither is it possible to create an instance of this class. They are usually used in classes that contain static members only. Some key points of a private constructor are:
- One use of a private constructor is when we have only static members.
- It provides an implementation of a singleton class pattern
- Once we provide a constructor that is either private or public or any, the compiler will not add the parameter-less public constructor to the class.
Now let's see it practically.
using System;
namespace defaultConstractor
{
public class Counter
{
private Counter() //private constrctor declaration
{
}
public static int currentview;
public static int visitedCount()
{
return ++ currentview;
}
}
class viewCountedetails
{
static void Main()
{
// Counter aCounter = new Counter(); // Error
Console.WriteLine("-------Private constructor example by vithal wadje----------");
Console.WriteLine();
Counter.currentview = 500;
Counter.visitedCount();
Console.WriteLine("Now the view count is: {0}", Counter.currentview);
Console.ReadLine();
}
}
}Now run the application. The output is as follows.

If you uncomment the preceding statement that is commented in the above program then it will generate an error because the constructor is inaccessible (private).
Summary
In this article, I discussed various types of constructors in C# language, I hope you found this article useful


Raviteja LPosted Apr 29, 2023, 10:35 AM EDT
Constructor Intialzies both public and private fields in a class.
Vikas SrivastavaPosted Jul 5, 2021, 1:29 AM EDT
Great explain.
Saktiprasad SwainPosted Mar 8, 2020, 11:12 PM EDT
Very Nice article.
Raju PaladiyaPosted Mar 8, 2020, 9:25 PM EDT
Nice one !!
Manisha JadhavPosted Mar 5, 2019, 5:58 PM EST
NIce Article
Metta ChittibabuPosted Feb 15, 2019, 3:53 AM EST
Nice article
suman paduchuriPosted Feb 11, 2019, 9:05 AM EST
Nice Article
Yogendra GuptaPosted Aug 31, 2018, 12:57 AM EDT
Nice explanation about constructors concept. :)
Nisar P VPosted May 13, 2018, 9:42 PM EDT
What is the use of 'class with private constructor' when we have abstract class for preventing instantiation?
Ashish MishraPosted May 2, 2018, 4:07 AM EDT
Nice concept explanation one of best ever i have read it
Azeem attariPosted Mar 23, 2018, 11:47 AM EDT
Very very very Informative article
Sagar Pandurang KapPosted Dec 12, 2017, 8:21 PM EST
Awesome Article.. Thank you .. Informative
Viksit SachdevaPosted Aug 11, 2017, 8:42 AM EDT
Very nice article.. thankyou
RakeshPosted Apr 11, 2017, 10:53 AM EDT
Great explanation. Thank you
Saurabh RaiPosted Jan 11, 2017, 7:34 PM EST
Nice article about the different type of constructors. Cleared many of my doubts.
SubashPosted Oct 19, 2016, 11:45 PM EDT
Nice explanation thank you so much
Vithal WadjeTOP 100Posted Mar 13, 2016, 12:07 PM EDT
Thanks monil thakor
Vithal WadjeTOP 100Posted Mar 13, 2016, 12:06 PM EDT
Thanks Rajeesh Menoth
Vithal WadjeTOP 100Posted Mar 13, 2016, 12:06 PM EDT
Thanks uzair hasan
Praveen ZagadePosted Mar 11, 2016, 10:07 AM EST
Would like to differ about the opinion on the private constructor. The article says that "When a constructor is created with a private specifier, it is not possible for other classes to derive from this class, neither is it possible to create an instance of this class." This may not be true. We can introduce another public parameterized constructor. This would open up the given Class for both object creation as well as inheritance. See the code snippet below: public class BaseClass { public static readonly string CONSTANT_STRING_1 = "CONSTANT_STRING_1"; public static readonly string CONSTANT_STRING_2 = "CONSTANT_STRING_2"; public int MyInt { get; set;} private BaseClass() {} public BaseClass(int a) { MyInt = a;} } public class DerivedClass { public int MyString{ get; set; } //DEFAULT CONSTRUCTOR NOT ALLOWED AS IT IS MARKED AS PRIVATE IN THE BASE CLASS //public DerivedClass() {} public DerivedClass(int a, string b): base(a) { MyString = b; } }
monil thakorPosted Mar 10, 2016, 9:25 PM EST
Nic
Rajeesh MenothPosted Aug 6, 2015, 3:44 AM EDT
Good One Sir :)
Santhosh KumarPosted Sep 29, 2014, 10:15 AM EDT
whether the compiler assign the default values to variables or "The default constructor initializes all numeric fields in the class to zero and all string and object fields to null " ??
uzair hasanPosted Mar 21, 2014, 10:27 PM EDT
good
Vithal WadjeTOP 100Posted Feb 3, 2014, 8:33 AM EST
thanks Adarsh sir
Adarsh BalachandranPosted Feb 3, 2014, 12:26 AM EST
Nice article Vithal sir, well defined!
Vishvajeet PawarPosted Dec 31, 2013, 10:44 PM EST
Chan Article..Vithal..!!
Vithal WadjeTOP 100Posted Oct 7, 2013, 10:05 AM EDT
thanks Shashank Srivastava sir i will see that
Shashank SrivastavaPosted Jun 27, 2013, 11:53 PM EDT
a query.. In case of "Copy Constructor", when instance constructor is defined, we have used "this" keyword with every variable, why is it not so in case when copy constructor is defined??