Can I inherit constructors?

You don’t need to create loads of constructors, all with the same code; you create only one, but have the derived classes call the base constructor:

public class Base
{
    public Base(Parameter p)
    {
        Init(p)
    }

    void Init(Parameter p)
    {
        // common initialisation code
    }
}

public class Derived : Base
{
    public Derived(Parameter p) : base(p)
    {
 
    }
}

Leave a Comment