Constructors in C#: (In simple Theory)
- Is a special type of function/method which has the same name as its
class.
- It is invoked automatically whenever an object of a class is created.
- It is also responsible for object initialization and memory
allocation of its class members.
- It doesn’t have a return a type, not
even a void type.
- Supports overloading by using parameterized constructor.
They are of 4 types:

Default Constructor:
The default constructor has no parameters. When a class has no constructor, default constructor is served by the compiler to that class. If you don’t define any constructor then default constructor is defined by the compiler. It is used to assign default values to instance variables of class
Example:
namespace Default_parameterizedConstructor
{
public class Student
{
// instance members
public int StudentId { get; set; }
public string Name { get; set; }
public string College { get; set; }
//default constructor parameters less
public Student()
{
College = "My College"; //explicit default value for college
}
}
class Program
{
static void Main(string[] args)
{
//Object instance of Student.
Student st = new Student();
Console.WriteLine("Student details are Student ID: {0}, Student Name: {1}, College :{2}", st.StudentId, st.Name, st.College);
Console.ReadKey();
}
}
}
The output for Default Constructor will be as: "Student details are Student ID: 0, Student Name: , College :My College"
Parameterized Constructor:
The parameterized constructor has one or more parameters. We use when we have to pass one or more parameter so that we can set the parameters of the class instance members. Used to assign values to instance variables of class. We use this keyword when the instance member name and the constructor receiving parameter are same in this case it is mandatory to specify this keyword. If names are different then it is optional to use this keyword.
Now, Lets take the above example with parameterized constructor:
namespace Default_parameterizedConstructor
{
public class Student
{
// instance members
public int StudentId { get; set; }
public string Name { get; set; }
public string College { get; set; }
//default constructor parameters less
public Student()
{
College = "My College"; //explicit default value for college
}
// parameterised constructor
public Student(int studentid, string name, string college)
{
this.StudentId = studentid;
this.Name = name;
this.College = college;
}
public Student(int studentid, string name)
{
this.StudentId = studentid;
this.Name = name;
}
}
class Program
{
static void Main(string[] args)
{
//default constructor parameter
//Object instance of Student.
Student st = new Student();
Console.WriteLine("The output for Default Constructor are Student ID: {0}, Student Name: {1}, College :{2}", st.StudentId, st.Name, st.College);
// parameterised constructor
//Operator overloading happening.
Student st1 = new Student(1, "John Doe", "MIT");
Console.WriteLine("The output for Default Constructor are Student ID: {0}, Student Name: {1}, College :{2}", st1.StudentId, st1.Name, st1.College);
// parameterised constructor with operator overloading
Student st2 = new Student(1,"Jimmy");
Console.WriteLine("The output for Default Constructor are Student ID: {0}, Student Name: {1}, College :{2}", st2.StudentId, st2.Name, st2.College);
Console.ReadKey();
}
}
}
*The Output will be as follows:
//First
The output for Default Constructor are Student ID: 0, Student Name: , College :My College
//Second
The output for Default Constructor are Student ID: 1, Student Name: John Doe, College :MIT
//Third
The output for Default Constructor are Student ID: 1, Student Name: Jimmy, College :*
Static Constructor:
A special type of constructor that gets called before the first object of the class is created.
Used to initialize any static fields, or to perform a particular action that need to perform only once.
A class can have only one static constructor and it must be a default constructor, having no access modifier.
Private Constructor:
Restrict a class external instantiation but in nested class you can create instance of this class.
In C# 1 X there was no static class, so developer used private constructor to prevent a class external instantiation.
Used to implement singleton pattern i.e. a single instance for a class.
A class can have multiple private constructors and public constructors.
If you don’t create an instance how will you access the value?
We can do that by making the member of the class as private static. Only static members are available using the class name.
Let look both by an example below:
namespace StaticAndPrivate
{
public class Example
{
private static int Counter;
//private constructor
private Example()
{
Counter = 10;
}
//static constructor
static Example()
{
Counter = 20;
}
//public constructor
public Example(int counter)
{
Counter = Counter + counter;
}
public static int GetCounter()
{
return ++Counter;
}
public class NestedExample
{
public void Test()
{
//internal instance
Example ex = new Example();
}
}
}
class Program
{
static void Main(string[] args)
{
//external instance
//Example ex = new Example();
Example ex = new Example(10);
Console.WriteLine("Counter : {0}", Example.GetCounter());
Console.ReadKey();
}
}
}
Hope this explains the concept with an example.