C# Inheritance & Constructor

Summary: in this tutorial, you’ll learn how C# calls the constructors of the base class and subclass.

The constructor calling order #

When you create a new instance of a subclass, C# calls the constructor of the base class first and then the constructor of the subclass. For example:

First, define a Person class with a parameterless constructor:

class Person
{ 
    public Person()
    {
        Console.WriteLine("Called the Person's constructor");
    }
}

Note that a parameterless constructor of a class is the constructor that has no parameter. If you don’t explicitly specify a parameterless constructor, the C# compiler will automatically generate one for the class.

Second, define an Employee class that inherits from the Person class:

class Employee : Person
{
    public Employee()
    {
        Console.WriteLine("Called the Employee's constructor");
    }

}

Third, create a new instance of the Employee class:

var employee = new Employee();

Output:

Called the Person's constructor
Called the Employee's constructor

As you can see from the output, C# executes the constructor of the Person class first and then the constructor of the Employee class.

In fact, C# always calls the parameterless constructor of the parent class. Consider the following example.

First, add a constructor with the name parameter to both Person and Employee classes:

class Person
{ 
    public Person()
    {
        Console.WriteLine("Called the Person's constructor");
    }

    public Person(string name)
    {
        Console.WriteLine("Called the Person's constructor with a parameter");
    }
}

class Employee : Person
{
    public Employee()
    {
        Console.WriteLine("Called the Employee's constructor");
    }

    public Employee(string name)
    {
        Console.WriteLine("Called the Employee's constructor with a parameter");
    }

}

Second, create a new instance of the Employee class:

var employee = new Employee("John Doe");

Output:

Called the Person's constructor
Called the Employee's constructor with a parameter

In this case, C# executes the parameterless constructor of the Person class (the base class) first and then the constructor with a parameter of the Employee class (the subclass).

If you remove the parameterless constructor of the Person class and/or the Employee class, you’ll get an error. For example:

class Person
{
    public Person(string name)
    {
        Console.WriteLine("Called the Person's constructor with a parameter");
    }
}

class Employee : Person
{
    public Employee(string name)
    {
        Console.WriteLine("Called the Employee's constructor with a parameter");
    }

}

Error:

There is no argument given that corresponds to the required formal parameter 'name' of 'Person.Person(string)'

In this example, the Person class doesn’t have a parameterless constructor. When you create a new instance of the Employee class, C# calls a parameterless constructor of the Person class.

However, it cannot find the parameterless constructor in the Person class. Therefore, it issues an error.

Change the calling order using base() #

To fix the issue, you can explicitly call a specific constructor of a parent class instead of letting C# call the parameterless constructor.

To do that, you use the base() after the declaration of the subclass’ constructor as follows:

class Person
{
    public Person(string name)
    {
        Console.WriteLine("Called the Person's constructor with a parameter");
    }
}

class Employee : Person
{
    public Employee(string name): base(name)
    {
        Console.WriteLine("Called the Employee's constructor with a parameter");
    }

}

Now, if you create a new instance of the Employee class:

// Program.cs

var employee = new Employee("John Doe");

C# will call the constructor of the Person class first and then the constructor of the Employee class:

Called the Person's constructor with a parameter
Called the Employee's constructor with a parameter

C# inheritance and constructor example #

First, define the Person class with a constructor that has three parameters:

// Person.cs
class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public byte Age { get; set; }

    public string FullName => $"{FirstName} {LastName}";

    public Person(string firstName, string lastName, byte age)
    {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
    }

    public string Introduce() => $"Hi, I'm {FullName}.";

}

Second, define the Employee with a constructor that has five parameters:

// Employee.cs
class Employee : Person
{
    public string JobTitle { get; set; }
    public decimal Salary { get; set; }

    public Employee(string firstName, string lastName, byte age, string jobTitle, decimal salary)
        : base(firstName, lastName, age)
    {
        JobTitle = jobTitle;
        Salary = salary;
    }
}

In this class, the Employee’s constructor calls the Person’s constructor using the base() syntax.

Summary #

  • In inheritance, C# always calls the parameterless constructor of the parent class unless you use the base() to call the specific constructor of the parent class.

Was this helpful?