I have a base class that has a large number of derived classes, and the base class has several constructors. In all my derived classes I have to implement empty constructors to forward the arguments to the base constructors. Is it possible to somehow tell C# to use the derived constructors?
For example, if using this base class.
class BaseTool
{
public BaseTool(string Arg1, string Arg2)
{
// do stuff.
}
public BaseTool(string Arg1)
{
// do stuff.
}
public BaseTool(int Arg1)
{
// do stuff.
}
}
I would have to implement all the above constructors with those arguments, and then call : base(...) to forward them to the derived class. This is resulting in a lot of classes that have empty constructors. Seems like a lot of wasted code.
Constructors are not inherited from base classes, so there is no way around declaring all constructors in each derived class. However, you could automate this repetitive task with, for example, T4 Templates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With