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
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted May 23, 2015, 6:34 AM EDT
Constructor chaining occurs through the use of inheritance A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.
There could be any number of classes in an inheritance chain. Every constructor method will call up the chain until the class at the top has been reached and initialized.
Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.
Examples:
{
string Stm;
public Class1()
{
}
public Class1(string st)
{
Stm = st;
Console.WriteLine("Class1 Constructor");
}
}
public class Class2 : Class1
{
string Stm;
public Class2(string st):base(st)
{
Stm = st;
Console.WriteLine("Class2 Constructor");
}
}
public class Class3 : Class2
{
string Stm;
public Class3(string st):base(st)
{
Stm = st;
Console.WriteLine("Class3 Constructor");
}
}
class Demo
{
static unsafe void Main(string[] args)
{
Class3 Cs = new Class3("Pankaj");
Console.ReadLine();
}
}
}
Abhishek KumarRank #1,735 · 872 reputationPosted Sep 8, 2015, 11:20 PM EDT