Passing parameters to the base class constructor

36,048

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.

Share:
36,048

Related videos on Youtube

n.y
Author by

n.y

Bachelor's degree, Computer Software Engineering. Master’s degree, Information Security. Currently a Programmer.

Updated on September 09, 2020

Comments

  • n.y
    n.y almost 4 years

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

    • catfood
      catfood about 10 years
      This is a duplicate question. stackoverflow.com/questions/12051/… Googling "base class constructor parameters" would have brought it right to you.
    • phoog
      phoog about 10 years
      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.
    • Chris Sinclair
      Chris Sinclair about 10 years
      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 with the derived constructors.
  • Ahmn21
    Ahmn21 about 4 years
    Is the default constructor still invoked if base constructor is called as :base(param) ?
  • BradleyDotNET
    BradleyDotNET about 4 years
    No. That constructor would have to call this() @Ahmn21