Thomas Nguyen
Thomas Nguyen

Reputation: 494

Use of base constructor

I'm having a problem with one of the C# applications I'm maintaining. I see something I don't understand so I would like to ask for clarification. It may or may not be related to the problem I'm having.

class C2iModel
{
    public C2iModel() { //blah }
}

class EplrsModel : C2iModel
{
    public EplrsModel() : base() { //blah }
}

My understanding is when a child constructor is invoked, the parent constructor is automatically invoked.

My question is does it make any difference whether the explicit call to base constructor is present in the EplrsModel constructor?

Upvotes: 1

Views: 136

Answers (2)

Ayush
Ayush

Reputation: 42450

In your example, calling the base constructor is redundant. You only need to explicitly specify that for constructors when you're passing in arguments. Example:

class Base {

  public Base(string type) { ... }
}

class Extend : Base {

  public Extend(string type, string name) : base(type) { ... }

}

Upvotes: 2

Ed Swangren
Ed Swangren

Reputation: 124642

Yes, that bit of code is redundant.

Upvotes: 2

Related Questions