C#C
C#9mo ago
yourFriend

Silly OOP question about constructors

I am learning OOP and came across this. A way to reuse constructors:
public Dog() : this("NoName") {}

public Dog(string name) : this(name, 1) {}

public Dog(string name, int age) : this(name, age, 0.3) {}

public Dog(string name, int age, double length) : this(name, age, length, new Collar()) {}

public Dog(string name, int age, double length, Collar collar)
{
    this.name = name;
    this.age = age;
    this.length = length;
    this.collar = collar;
}


I was wondering that can I just use default values for parameters like this, to achieve same result:
public Dog(string name="NoName", int age=1, double length=0.3, Collar collar=new Collar())
{
    this.name = name;
    this.age = age;
    this.length = length;
    this.collar = collar;
}


If yes then is their any use case when we would prefer the 1st way over 2nd?
Screenshot_509.png
Was this page helpful?