24

If the base class and derived class both have their constructors with parameters then where we pass the parameters to the base class constructors?

n.y
  • 3,343
  • 3
  • 35
  • 54
  • This is a duplicate question. http://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp Googling "base class constructor parameters" would have brought it right to you. – catfood May 05 '14 at 20:59
  • 1
    You say "you can't instantiate an abstract class," but that's not entirely true. If you create an instance of the non-abstract derived class, that instance is also an instance of the abstract base class. It is also an instance of the System.Object class. – phoog May 05 '14 at 21:05
  • 1
    FYI, even if it's an `abstract` class, it can have defined custom constructors with parameters that can be passed in. They can't be instantiated/called _directly_ but instead are referenced in derived types by using the [`base` keyword](http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx) with the derived constructors. – Chris Sinclair May 05 '14 at 21:05

1 Answers1

62

Like this:

public class DerivedClass : BaseClass
{
    public DerivedClass(int derivedParam, String baseParam):base(baseParam)
    {
    }
}

The base keyword here calls the base class constructor that matches the provided parameter overload.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117