n.y
n.y

Reputation: 3490

Passing parameters to the base class constructor

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

Upvotes: 24

Views: 51553

Answers (2)

armyllc
armyllc

Reputation: 176

public class DerivedClass(object param): BaseClass(param){// in .net core 8}

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61349

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.

Upvotes: 64

Related Questions