I know this doesn’t directly answer your question; however, if most of your constructors simply introduce a new parameter on the previous constructor, then you could take advantage of optional arguments (introduced in C# 4) to reduce the number of constructors you need to define.
For example:
public class BaseClass
{
private int x;
private int y;
public BaseClass()
: this(0, 0)
{ }
public BaseClass(int x)
: this(x, 0)
{ }
public BaseClass(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class DerivedClass : BaseClass
{
public DerivedClass()
: base()
{ }
public DerivedClass(int x)
: base(x)
{ }
public DerivedClass(int x, int y)
: base(x, y)
{ }
}
The above can be reduced to:
public class BaseClass
{
private int x;
private int y;
public BaseClass(int x = 0, int y = 0)
{
this.x = x;
this.y = y;
}
}
public class DerivedClass : BaseClass
{
public DerivedClass(int x = 0, int y = 0)
: base(x, y)
{ }
}
And it would still allow you to initialize both BaseClass
and DerivedClass
with any number of arguments:
var d1 = new DerivedClass();
var d2 = new DerivedClass(42);
var d3 = new DerivedClass(42, 43);