Constructor chaining is one of the most important Object Oriented Programming concepts in C#. It allows one constructor to call another constructor inside the same class using the this() keyword. This helps avoid duplicate code and keeps object initialization clean and consistent.
Here is the complete video where I explain constructor chaining step by step with this exact program:
namespace ConstructorChaining
{
internal class Program
{
static void Main(string[] args)
{
Person p = new Person("Sudipto","Male",DateTime.Parse("07-Jul-1980"));
p.Display();
Console.ReadKey();
}
}
class Person
{
private int personId;
private string personName;
private string gender;
private DateTime dob;
// Default constructor
public Person()
{
this.personId = DateTime.Now.Microsecond;
}
// Parameterized constructor 1
public Person(string personName) : this()
{
this.personName = personName;
}
// Parameterized constructor 2
public Person(string personName, string gender) : this(personName)
{
this.gender = gender;
}
// Parameterized constructor 3
public Person(string personName, string gender, DateTime dob) : this(personName, gender)
{
this.dob = dob;
}
public void Display()
{
Console.WriteLine("Person Id: " + this.personId);
Console.WriteLine("Name: " + this.personName);
Console.WriteLine("Gender: " + this.gender);
Console.WriteLine("DOB: " + this.dob);
}
}
}
When the following line executes:
Person p = new Person("Sudipto","Male",DateTime.Parse("07-Jul-1980"));
C# does not directly jump into the 3-parameter constructor. Instead, it follows this chain:
Person() (default constructor)Person(string)Person(string, string)Person(string, string, DateTime)This happens because each constructor uses the this() keyword to forward the call to another constructor. So every object gets properly initialized step by step.
Without constructor chaining, you would need to repeat initialization logic like setting personId in every constructor.
That leads to:
Using constructor chaining ensures that shared initialization logic is written only once and reused safely.
I am Sudipto, a Microsoft-certified .NET trainer with over 20 years of experience. I provide 1-to-1 online and offline training in C#, ASP.NET Core, SQL Server, Web API and interview preparation.
Website: https://supernovaservices.com
WhatsApp: +91-9331897923
If you want to truly understand C# and .NET from first principles instead of just memorizing interview questions, feel free to contact me.
To keep every session productive and distraction-free, please follow these simple guidelines:
Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.
I prefer to start with a short 10-minute free call so I can understand:
Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.
Note: Payment is made only after your first class, once you’re completely satisfied. However, fees paid after the first class are non-refundable. This helps maintain scheduling commitments and allows me to reserve your preferred time slot with full attention.