Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
sasikala sRank #1,091 · 1.7K reputationPosted Jul 3, 2025, 1:22 AM EDT
Constructor chaining in C# .NET is a technique where one constructor calls another constructor within the same class or a base class. This is primarily used to reduce code duplication and improve code reusability, as common initialization logic can be placed in a single constructor and then called by others.
Sangeetha SRank #212 · 11.4K reputationPosted Jul 3, 2025, 2:29 AM EDT
Constructor Chaining in C# is a technique where one constructor calls another constructor in the same class or in the base class. This helps avoid code duplication and keeps initialization logic centralized.
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Jul 3, 2025, 7:39 AM EDT
https://www.c-sharpcorner.com/article/constructor-chaining2/
Tuhin PaulRank #7 · 71.9K reputationPosted Jul 3, 2025, 3:57 PM EDT
Why Use Constructor Chaining?
Chaining Within the Same Class (
this) :thiskeyword.thiskeyword must be the first statement in the constructor.Chaining to a Base Class Constructor (
base) :basekeyword.this, thebasekeyword must also appear as the first statement in the constructor.using System; class Person { public string Name { get; set; } public int Age { get; set; } // Default constructor public Person() : this("Unknown", 0) // Chains to the parameterized constructor { Console.WriteLine("Default constructor called."); } // Parameterized constructor public Person(string name, int age) { Name = name; Age = age; Console.WriteLine($"Parameterized constructor called: Name = {Name}, Age = {Age}"); } } class Employee : Person { public string Department { get; set; } // Default constructor public Employee() : base("Employee", 25) // Chains to the base class constructor { Department = "Unknown"; Console.WriteLine("Employee default constructor called."); } // Parameterized constructor public Employee(string name, int age, string department) : base(name, age) // Chains to the base classPerson Class :
Personclass has two constructors:this("Unknown", 0).NameandAgeproperties.Employee Class :
Employeeclass inherits fromPersonand adds aDepartmentproperty.base("Employee", 25).base(name, age).Execution Flow :
Personobject, the default constructor calls the parameterized constructor, which initializes the properties.Employeeobject:Departmentproperty.Departmentproperty.Constructor chaining is especially useful in inheritance hierarchies where derived classes need to initialize properties defined in the base class.