I have few types that derive from simplified Base
as shown below.
I am not sure whether to use base class's constructor or this
constructor when overloading constructors.
ConcreteA
overloads constructors purely using base
constructors, while
ConcreteB
overloads using this
for the first two overloads.
What would be a better way of overloading constructors?
public abstract class Base
{
public string Name { get; set; }
public int? Age { get; set; }
protected Base() : this(string.Empty) {}
protected Base(string name) : this(name, null) {}
protected Base(string name, int? age)
{
Name = name;
Age = age;
}
}
public class ConcreteA : Base
{
public ConcreteA(){}
public ConcreteA(string name) : base(name) {}
public ConcreteA(string name, int? age) : base(name, age)
{
}
}
public class ConcreteB : Base
{
public ConcreteB() : this(string.Empty, null){}
public ConcreteB(string name): this(name, null){}
public ConcreteB(string name, int? age) : base(name, age)
{
}
}
[Edit]
It looks like what Ian Quigley has suggested in his answer seemed to make sense.
If I were to have a call that initialize validators, ConcreteA(string)
will never initialize validators in following case.
public class ConcreteA : Base
{
public ConcreteA(){}
public ConcreteA(string name) : base(name) {}
public ConcreteA(string name, int? age) : base(name, age)
{
InitializeValidators();
}
private void InitializeValidators() {}
}