1. What is a Constructor in C#?
What is it?
A constructor is a special method in a class that is automatically executed when an object is created.
The main purpose of a constructor is to initialize an object's fields and properties.
Key characteristics
-
Constructor name must be the same as the class name.
-
It does not have a return type, not even
void. -
It is automatically called when an object is created using
new(for ordinary class instantiation). -
Constructors can accept parameters.
-
Constructors can be overloaded.
-
Constructors cannot be inherited or overridden.
Simple Example
public class Employee
{
public string Name;
// Constructor
public Employee()
{
Name = "John";
Console.WriteLine("Constructor Executed");
}
}
Employee emp = new Employee();
Console.WriteLine(emp.Name);
Output:
Constructor Executed
John
Explanation:
-
new Employee()creates an Employee object. -
The constructor
Employee()executes automatically. -
It initializes
Namewith"John". -
The object is ready for use.
Execution Flow
Employee emp = new Employee();
|
v
Object Creation
|
v
Constructor Called
|
v
Initialize Name
|
v
Object Ready
2. What are the Different Types of Constructors in C#?
The commonly discussed constructor types are:
-
Default Constructor
-
Parameterless Constructor
-
Parameterized Constructor
-
Copy Constructor
-
Static Constructor
-
Private Constructor
2.1 Default Constructor
A default constructor is a parameterless constructor automatically provided by the compiler when a class does not declare any instance constructor.
Example
public class Employee
{
public int Id;
public string? Name;
}
Employee emp = new Employee();
Console.WriteLine(emp.Id);
Console.WriteLine(emp.Name);
Output:
0
The second line is blank because Name is null.
Explanation:
-
No constructor is explicitly defined.
-
C# automatically provides a public parameterless constructor.
-
Fields receive their default values.
Key Points:
-
Automatically generated when no instance constructor is declared.
-
intdefaults to0,booltofalse, and reference types tonull. -
If you declare a parameterized constructor, the compiler does not automatically add a parameterless constructor.
2.2 Parameterless Constructor
A parameterless constructor is explicitly defined by the developer without parameters.
Example
public class Employee
{
public string Name;
public Employee()
{
Name = "Default Employee";
}
}
Employee emp = new Employee();
Console.WriteLine(emp.Name);
Output:
Default Employee
Explanation:
The constructor initializes the Name field with a predefined value.
Key Points:
-
Does not accept parameters.
-
Useful for assigning initial values.
-
Unlike a compiler-generated default constructor, it can contain custom initialization logic.
2.3 Parameterized Constructor
A parameterized constructor accepts arguments to initialize an object with specific values.
Example
public class Employee
{
public int Id;
public string Name;
public Employee(int id, string name)
{
Id = id;
Name = name;
}
}
Employee emp = new Employee(101, "John");
Console.WriteLine(emp.Id);
Console.WriteLine(emp.Name);
Output:
101
John
Explanation:
-
101is passed toid. -
"John"is passed toname. -
These values initialize the object's fields.
Key Points:
-
Accepts one or more parameters.
-
Allows different objects to have different initial values.
-
Frequently used in real-world applications and Dependency Injection.
2.4 Copy Constructor
A copy constructor creates a new object by copying values from another object of the same class.
C# does not automatically generate copy constructors for ordinary classes; developers implement them.
Example
public class Employee
{
public int Id;
public string Name;
public Employee(int id, string name)
{
Id = id;
Name = name;
}
// Copy Constructor
public Employee(Employee emp)
{
Id = emp.Id;
Name = emp.Name;
}
}
Employee emp1 = new Employee(101, "John");
Employee emp2 = new Employee(emp1);
Console.WriteLine(emp2.Id);
Console.WriteLine(emp2.Name);
Output:
101
John
Execution Flow
emp1
+----------------+
| Id = 101 |
| Name = John |
+----------------+
|
| Copy Constructor
v
emp2
+----------------+
| Id = 101 |
| Name = John |
+----------------+
Key Points:
-
Creates a separate object.
-
Copies values from an existing object.
-
The example copies value-type data and an immutable string.
-
For mutable reference-type fields, additional logic is needed to create a deep copy.
2.5 Static Constructor
A static constructor initializes static members of a class.
It executes automatically, at most once per type initialization, before the type's first use that requires initialization.
Example
public class Employee
{
public static string CompanyName;
// Static Constructor
static Employee()
{
CompanyName = "ABC Technologies";
Console.WriteLine("Static Constructor Called");
}
public Employee()
{
Console.WriteLine("Instance Constructor Called");
}
}
Employee emp1 = new Employee();
Employee emp2 = new Employee();
Output:
Static Constructor Called
Instance Constructor Called
Instance Constructor Called
Explanation:
-
The static constructor executes only once.
-
The instance constructor executes for each new object.
-
Both objects share the same static
CompanyName.
Execution Flow
new Employee()
|
v
Static Constructor (Once)
|
v
Instance Constructor
|
v
First Object Created
new Employee()
|
v
Instance Constructor
|
v
Second Object Created
Key Points:
-
Executes automatically only once per type initialization.
-
Cannot accept parameters or access modifiers.
-
Cannot be called explicitly.
-
Used to initialize static fields or perform one-time initialization.
2.6 Private Constructor
A private constructor prevents external classes from directly creating an object using that constructor.
Example
public class DatabaseConnection
{
private DatabaseConnection()
{
Console.WriteLine("Private Constructor");
}
public static void Connect()
{
Console.WriteLine("Database Connected");
}
}
// Cannot create object from outside
// DatabaseConnection db = new DatabaseConnection();
DatabaseConnection.Connect();
Output:
Database Connected
Explanation:
-
The constructor is private.
-
External classes cannot call
new DatabaseConnection(). -
The static
Connect()method can be called without creating an object.
Key Points:
-
Restricts object creation from outside the class.
-
Commonly used in Singleton implementations.
-
Useful in utility classes that should not be instantiated.
-
A class with only private constructors cannot be instantiated directly by external code.
3. Constructor Overloading
Constructor overloading means defining multiple constructors in the same class with different parameter lists.
Example
public class Employee
{
public int Id;
public string Name;
public Employee()
{
Id = 0;
Name = "Unknown";
}
public Employee(int id)
{
Id = id;
Name = "Unknown";
}
public Employee(int id, string name)
{
Id = id;
Name = name;
}
}
// Different constructors
Employee emp1 = new Employee();
Employee emp2 = new Employee(101);
Employee emp3 = new Employee(102, "John");
Explanation:
|
Object |
Constructor Called |
|---|---|
|
|
|
|
|
|
|
|
|
Key Points:
-
Multiple constructors can exist in the same class.
-
Each constructor must have a different parameter signature.
-
Constructor overloading is an example of compile-time polymorphism.
-
It provides flexibility when initializing objects.
4. Constructor Chaining
Constructor chaining means calling one constructor from another constructor using this() or base().
Example
public class Employee
{
public int Id;
public string Name;
public Employee() : this(0, "Unknown")
{
Console.WriteLine("Default Constructor");
}
public Employee(int id, string name)
{
Id = id;
Name = name;
Console.WriteLine("Parameterized Constructor");
}
}
Employee emp = new Employee();
Output:
Parameterized Constructor
Default Constructor
Explanation:
The parameterless constructor calls the parameterized constructor using this(0, "Unknown").
new Employee()
|
v
Employee()
|
| this(0, "Unknown")
v
Employee(int id, string name)
|
v
Initialize Fields
|
v
Return to Employee()
|
v
Object Ready
Key Points:
-
this()calls another constructor in the same class. -
base()calls a constructor in the base class. -
Helps avoid duplicate initialization code.
-
A constructor can specify only one constructor initializer.
5. Constructor vs Normal Method
|
Feature |
Constructor |
Normal Method |
|---|---|---|
|
Name |
Same as class |
Any valid method name |
|
Return type |
No return type |
Must declare a return type |
|
Execution |
Automatically during object creation |
Called explicitly |
|
Purpose |
Initialize object |
Perform operations |
|
Overloading |
Supported |
Supported |
|
Inheritance |
Not inherited |
Can be inherited |
|
Overriding |
Not supported |
Supported for virtual methods |
6. Constructor Types – Quick Revision
|
Constructor |
Purpose |
Parameters |
|---|---|---|
|
Default |
Compiler-provided initialization |
No |
|
Parameterless |
Custom initialization without arguments |
No |
|
Parameterized |
Initialize with supplied values |
Yes |
|
Copy |
Copy another object's values |
Object parameter |
|
Static |
Initialize static members |
No |
|
Private |
Restrict object creation |
Can have parameters |
7. Important Interview Questions
Q1. Can a constructor return a value?
No. A constructor cannot declare a return type. It initializes an object rather than returning a value like a normal method.
Q2. Can a constructor be overloaded?
Yes. Multiple constructors can have different parameter lists.
Q3. Can a constructor be inherited?
No. Constructors are not inherited, but derived-class constructors can call base-class constructors using base().
Q4. Can a constructor be virtual, abstract, or overridden?
No. Constructors cannot be declared virtual, abstract, or override.
Q5. Can an abstract class have a constructor?
Yes. An abstract class can have constructors, which execute when a derived-class object is created.
Q6. Can an interface have a constructor?
An interface cannot declare an instance constructor. It does not directly create instances.
Q7. What happens if we do not define any constructor?
For an ordinary class, C# automatically provides a public parameterless constructor.
Q8. What is the difference between a static constructor and an instance constructor?
A static constructor runs once for type initialization. An instance constructor runs whenever a new object is created.
Q9. Can a constructor be asynchronous?
No. Constructors cannot use the async modifier. Asynchronous initialization is typically implemented using an async factory method.
Q10. Which constructor is commonly used in ASP.NET Core Dependency Injection?
A parameterized constructor is commonly used for constructor injection.
Example:
public class EmployeeController : ControllerBase
{
private readonly IEmployeeService _service;
public EmployeeController(IEmployeeService service)
{
_service = service;
}
}
ASP.NET Core's DI container resolves IEmployeeService and passes the service instance to the controller constructor.
Interview takeaway: Focus especially on parameterized constructors, constructor overloading, static constructors, constructor chaining, and constructor injection. These are important concepts for C# and ASP.NET Core technical interviews.