0

Say you have a parent class with lots of subclasses. If the constructor of the parent gets a new parameter you would have to change all the children constructors, too. Is there a way to inherit the parent constructor without naming all the parameters?

Code that I want to write, but gives the error 'There is no argument given for the formal parameter...':

public abstract class A
{
    public A(someType somePar)
    {
    }
}

public class B : A
{
}

Annoying solution:

public abstract class A
{
    public A(someType somePar)
    {
    }
}

public class B : A
{ 
   //If there is a new parameter, this would need to change
   B(someType somePar) : base(somePar)
   {
   }
}

Sorry if there is an obvious solution to this, I'm writing for the first time with C#

Martin Clever
  • 147
  • 1
  • 11
  • 2
    [Optional arguments](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments) – GSerg Aug 21 '21 at 08:54
  • 2
    Have your base class take a specific class as a parameter (rather than say a string or an int) with all of the parameters instead as _properties_ of the class.. If you add a new property to the class, you don't need to change anything in terms of the `base` calls. – mjwills Aug 21 '21 at 08:55
  • @mjwills That is a great idea, thanks! – Martin Clever Aug 21 '21 at 08:59
  • Without optional argument there is no solution because you need to propagate constructors except the default without any parameter unless privatized in an ancestor to remove it in childs. [Constructors (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors) • [Using Constructors (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors) –  Aug 21 '21 at 09:02
  • If the new parameter in the base class constructor is not required, why don't just create also a parameterles constructor in the base class? – Steeeve Aug 21 '21 at 09:04
  • 1
    And when mjwills says "specific class" they don't mean one of the concrete subclasses but some other class that's just a holder for data that an A or B can use during construction. Kinda like a ProcessStartInfo is a holder for info that is used by the Process.Start method. Microsoft could add a property to ProcessStartInfo without needing to change the signature of Process.Start method – Caius Jard Aug 21 '21 at 09:04
  • 1
    Does this answer your question? [C# does not inherit the constructor from base class](https://stackoverflow.com/questions/4544382/) and [How to inherit constructors?](https://stackoverflow.com/questions/223058/) and [Can I inherit constructors?](https://stackoverflow.com/questions/3873343/) –  Aug 21 '21 at 09:06
  • 2
    Yeah, I think I understand it now. Thanks for all your answers. – Martin Clever Aug 21 '21 at 09:08
  • 1
    @MartinClever It's a little different from C++ and Delphi, and it confused and even annoyed me at first and still today when I switched to C#. It is boring, but even without multiple inheritance it has become my favorite language. Can't wait for the diamond operator to do true polymorphism on open generic types. –  Aug 21 '21 at 09:11

0 Answers0