what are the advantages of calling one constructor by other if there are multiple constructors? Thanks
-
3Well, what are the advantages of calling one *method* from another? Constructors are just *methods* that are called before the result of the `new` operator is produced. – Eric Lippert Feb 08 '12 at 16:05
5 Answers
You don't repeat yourself.
A change in implementing one constructor also affects all the other constructors, instantly. Copy and Pasting code is bad and should be avoided.

- 3,892
- 8
- 38
- 63
-
Related: http://stackoverflow.com/questions/2490884/why-is-copy-and-paste-of-code-dangerous/2490897#2490897 – Oded Feb 08 '12 at 15:52
the same advantages you get when you do method overloading : you don't repeat the same code
public class Person
{
public Person(string name,string lastName )
{
Name = name;
LastName = lastName;
}
public Person(string name, string lastName,string address):this(name,lastName)
{
//you don't need to set again Name and Last Name
//as you can call the other constructor that does the job
Address = Address;
}
public string Name { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
}

- 26,379
- 6
- 61
- 70
Looking at the already posted answers i will just they that you always walk the way from the default constructor down to the most specialized constructor. Trying to do the same the other way around always leads to code duplications or problems:
The good way:
public class Foo()
{
public Foo()
: this(String.Empty)
{ }
public Foo(string lastName)
: this(lastName, String.Empty)
{ }
public Foo(string lastName, string firstName)
: this(lastName, firstName, 0)
{ }
public Foo(string lastName, string firstName, int age)
{
LastName = lastName;
FirstName = firstName;
Age = age;
_SomeInternalState = new InternalState();
}
}
The bad way:
public class Foo()
{
public Foo(string lastName, string firstName, int age)
: this(lastName, firstName)
{
Age = age;
}
public Foo(string lastName, string firstName)
: this(lastName)
{
FirstName = firstName;
}
public Foo(string lastName)
: this()
{
LastName = lastName;
}
public Foo()
{
_SomeInternalState = new InternalState();
}
}
The problem of the second example is that the part what to do with all the parameters is now cluttered over all constructors, instead implemented in just one (the most specialized). Just imagine you like to derive from this class. In the second example you have to override all constructors. In the first example you only have to override the most specialized constructor to get full control over every constructor.

- 43,366
- 8
- 94
- 151
-
I'm not a fan of this example. You probably should cascade the other way. From the highest # of arguments to the lowest – Kyle Feb 09 '12 at 00:08
-
2@Kyle: NNNOOOOOOOO. Doing it as you proposed leads to problems. See my updated answer. – Oliver Feb 09 '12 at 08:20
-
Not sure I get the point...if `Foo` is a parent class and you have class `Bar : Foo` and you want to implement `Bar(last, first) : base(last, first)` I don't see a problem with either method. I'll say the first way can be nice to have all your parameters set in one place just for looking's sake but it doesn't cover *all* cases you might want to chain constructors. Imagine two constructors that are call different methods during setup that have a common "base" implementation but should not call each other – Assimilater May 23 '17 at 21:57
If you want to pass default values to a base constructor.
public class YourClass
{
private int SomeInt;
public YourClass() : this(0)
{
// other possible logic
}
public YourClass(int SomeNumber)
{
SomeInt = SomeNumber;
}
}
This follows the DRY principle (Don't Repeat Yourself). A simple example, but it should illustrate the idea.
I used it when I want to pass default or null values to the other constructors. In the case above the user does not have to pass null when calling the constructor-- they can call it with nothing.
public class Widget(){
public Widget() : this(null){
}
public Widget(IRepository rep){
this.repository = rep;
}
}

- 10,153
- 6
- 47
- 60