How can I call base class constructor from derived class in C#?


I would like to know, suppose if I have inherited a Class A in Class B then How can I call the constructor of the base class(Class A) in C#?


Asked by:- Vinnu
0
: 10158 At:- 5/2/2018 9:29:46 AM
C# Call base constructor







2 Answers
profileImage Answered by:- Sam

yes you can call base class constructor from derived class in C#, In the inheritance hierarchy, always the base class constructor is called first. In c#, the base keyword is used to access the base class constructor as shown below.

We have used the ':base(...)' keyword after the constructor declaration with a specific parameter list.

Take a look the below example:

using System;
public class Person
{
    protected string ssn = "111-222-333";
    protected string name = "John Wick";

    public virtual void GetInfo()
    {
        Console.WriteLine("Name: {0}", name);
        Console.WriteLine("SSN: {0}", ssn);
    }
}
public class Employee : Person
{
    public string id = "ABC123";
    public override void GetInfo()
    {
        // Calling the base class GetInfo method:
        base.GetInfo();
        Console.WriteLine("Employee ID: {0}", id);
    }
}

public class TestClass
{
    public static void Main()
    {
        Employee E = new Employee();
        E.GetInfo();
    }
}

In the above example, both the base class, Person, and the derived class, Employee, have a method named Getinfo. By using the base keyword, it is possible to call the Getinfo method on the base class, from within the derived class.

Working fiddle : https://dotnetfiddle.net/CB7KgN

Output of the above code will be

Name: John Wick
SSN: 111-222-333
Employee ID: ABC123

base-class-contructor-call-in-c-sharp-min.png

Here is the another working example:

using System;

public class A // This is the base class.
{
    public A(int value)
    {
        // Executes some code in the constructor.
        Console.WriteLine("Base constructor A()");
    }
}

public class B : A // This class derives from the previous class.
{
    public B(int value)
        : base(value)
    {
        // The base constructor is called first.
        // ... Then this code is executed.
        Console.WriteLine("Derived constructor B()");
    }
}

class Program
{
    static void Main()
    {
        // Create a new instance of class A, which is the base class.
        // ... Then create an instance of B.
        // ... B executes the base constructor.
        A a = new A(0);
        B b = new B(1);
    }
}

Output:

Base constructor A()
Base constructor A()
Derived constructor B()

Note:Class A and class B both introduce constructors. Class A is the parent or base class for class B, which is referred to as the derived class. 

In the above example, the constructor in class B calls into the constructor of class A using base initializer syntax. As we specify that the base class constructor is called upon entry to the derived constructor. In the B constructor, we use base initializer syntax.The compiler inserts the constructor call at the start of the method body.

2
At:- 5/2/2018 11:58:19 AM Updated at:- 5/2/2018 12:08:25 PM


profileImage Answered by:- vikas_jk

Above details are correct, if you want to learn more about C# inheritance, read this tutorial

https://qawithexperts.com/tutorial/c-sharp/23/c-sharp-inheritance

Above tutorial explains more about Inheritance in C#.

0
At:- 1/7/2020 8:43:58 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use