What is constructor chaining?

 Posted by Rajni.Shekhar on 4/9/2012 | Category: C# Interview questions | Views: 1665 | Points: 40
Answer:

Constructor Chaining is used to call a constructor in another constructor. Means you can initialize some fields using another constructor in one constructor.

Example:

Public class Example{
string strName;
string strCity;

public Example(string Name, string City)
{
this.strName = FirstName;
this.strCity = City;
}
//another constructor
public Example(string Name):this(Name,"Delhi")
{
this.strName= FName;
}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response