Ahammad Fekri
Ahammad Fekri

Reputation: 31

Class to inherit the constructor of its base class

I would like to know if I can access the constructor of the base class in its derived classes in C#. If yes please let me know how could we make it. Thanks in advance.

Upvotes: 0

Views: 135

Answers (1)

Eric J.
Eric J.

Reputation: 150108

You can call the base class constructor as part of the execution of the derived class constructor

public MyBase
{
    public MyBase() { }
}

public Derived
{
    public Derived() : base() { }
}

When using this pattern, you are said to be using the base class initializer.

For more background, see the base keyword and instance constructors on MSDN.

Upvotes: 1

Related Questions