Chain Constructor:
Constructor Chaining is an approach where a Constructor calls another Constructor in the
same or base class.
Example of chain Constructor code snippet
namespace ConstructoreChaining
{
class A
{
public A()
{
Console.WriteLine("ConstructorExampleExample A.");
}
public A(string s){
Console.WriteLine("ConstructorExample A with parameter = {0}",s);
}
public A(string s,string t)
{
Console.WriteLine("ConstructorExample A with parameter = {0} & {1}", s,t);
}
}
class B:A
{
public B():base()
{
Console.WriteLine("ConstructorExample B.");
}
public B(string s):base(s)
{
Console.WriteLine("ConstructorExample B with parameter = {0}", s);
}
public B(string s, string t):base(s,t)
{
Console.WriteLine("ConstructorExample B with parameter = {0} &{1}", s, t);
}
}
class Program
{
static void Main(string[] args)
{
B b1 = new B();
B b2 = new B("First Parameter ", "Second Parameter");
Console.Read();
}
}
}