If you ask the question that can we call constructor from another constructor then I will say yes we can. Calling constructor from another constructor is known as constructor chaining . Constructor Chaining can be done by the keyword this and base. Here we discuss on this keyword and will discuss base keyword in the article base class constructor calling from derived class.
In the following class we have created two constructor. when we call default constructor then it is calling parameterized constructor and assign value to variable.
public class MyClass
{
public string companyName;
public MyClass():this("XYZ Company")
{
}
public MyClass(string compname)
{
companyName = compname;
}
}
Now create the object of class and call the default constructor and display the variable.
MyClass mc = new MyClass();
Response.Write(mc.companyName);
In this way constructor will call another constructor.
Note:-Static constructor cannot be chained. And a public constructor can access a private constructor with the help of constructor chaining.