What is the Constructor Chaining in C#?
What is the Constructor Chaining in C#?
Constructor chaining is an approach to connect two or more classes in a relationship as Inheritance. In Constructor Chaining, one constructor invoked from another constructor implicitly by base keyword so when you create an instance of the child class to it will call parent's class Constructor without it inheritance is not possible. Or in other words, every child class Constructor is mapped to parent class Constructor.
class Employee
{
string _department="";
string _id="";
string _fName="";
string _lName="";
public Employee(string id)
{
_department="<Employee_Department>";
_id=id;
}
public Employee(string id,string fName)
{
_department="<Employee_Department>";
_id=id;
_fName=fName;
}
public Employee(string id,string fName,string lName)
{
_department="<Employee_Department>";
_id=id;
_fName=fName;
_lName=lName;
}
}
class Employee
{
string _department="";
string _id="";
string _fName="";
string _lName="";
public Employee(string id)
:this(id,"","") {
}
public Employee(string id,string fName)
:this(id,"fName","") {
}
public Employee(string id,string fName,string lName)
{
_department="<Employee_Department>";
_id=id;
_fName=fName;
_lName=lName;
}
}
Hope its informative...
Please log in or register to add a comment.