C# - Making All Derived Classes Call the Base Class Constructor

C# - Making all derived classes call the base class constructor

You do have to redeclare constructors, because they're effectively not inherited. It makes sense if you think of constructors as being a bit like static methods in some respects.

In particular, you wouldn't want all constructors to be automatically inherited - after all, that would mean that every class would have a parameterless constructor, as object itself does.

If you just want to call the base class constructor though, you don't need to write any code in the body of the constructor - just pass the arguments up to the base class as per Waleed's post.

If your base class starts requiring more information, it's natural that you should have to change all derived classes - and indeed anything calling the constructors of those classes - because they have to provide the information. I know it can seem like a pain, but it's just a natural consequence of what constructors do.

Letting implicit derived class constructor call base class constructor

It already does; let's run your code through sharplab.io and look at the IL for B:

.class public auto ansi beforefieldinit B
extends A
{
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2058
// Code size 7 (0x7)
.maxstack 8

IL_0000: ldarg.0
IL_0001: call instance void A::.ctor()
IL_0006: ret
} // end of method B::.ctor

} // end of class B

we can see here that B has a public parameterless constructor that calls A's parameterless constructor (on IL_0001). Essentially,

public class B : A {
}

is short-hand for

public class B : A {
public B() : base() {}
}

Is it possible to call a derived class constructor from a base class constructor?

Short answer: no. That would be breaking the object-oriented paradigm, so you wouldn't want to be able to do that anyway. Think about it this way: an abstract base class can be extended by an arbitrary number of classes, but if it was tightly coupled to one of the child classes, how would that impact the other ones?

public class BaseClass {
// Call child class constructor
public BaseClass() : A() { }
}

public class A : BaseClass {
public A() { ... }
}

// How should BaseClass handle this? There is no constructor named "A."
public class B : BaseClass {
public B() { ... }
}

If you want the base class and derived class to share some functionality, you should make it a protected method in the base class. That way, you can call that method from the constructor.

You could also make a constructor on the base class that provides the common functionality and call it from the child class with : base(...).

Why do derived classes need to use a base class constructor

I think it's because the derived class needs to create an object of the base class but why?

An instance of a derived class is an instance of the base class. If you have a rule for what must happen when you construct an Animal, and you're constructing a Giraffe, then somehow you have to execute the rule for constructing an Animal. In C# that mechanism is "call a base class constructor".

Calling the base constructor in C#

Modify your constructor to the following so that it calls the base class constructor properly:

public class MyExceptionClass : Exception
{
public MyExceptionClass(string message, string extrainfo) : base(message)
{
//other stuff here
}
}

Note that a constructor is not something that you can call anytime within a method. That's the reason you're getting errors in your call in the constructor body.

Will the base class constructor be automatically called?

This is simply how C# is going to work. The constructors for each type in the type hierarchy will be called in the order of Most Base -> Most Derived.

So in your particular instance, it calls Person(), and then Customer() in the constructor orders. The reason why you need to sometimes use the base constructor is when the constructors below the current type need additional parameters. For example:

public class Base
{
public int SomeNumber { get; set; }

public Base(int someNumber)
{
SomeNumber = someNumber;
}
}

public class AlwaysThreeDerived : Base
{
public AlwaysThreeDerived()
: base(3)
{
}
}

In order to construct an AlwaysThreeDerived object, it has a parameterless constructor. However, the Base type does not. So in order to create a parametersless constructor, you need to provide an argument to the base constuctor, which you can do with the base implementation.



Related Topics



Leave a reply



Submit