How to explicitly call base class constructor from child class in C#?

The base keyword in C# is used to explicitly call a constructor from the parent class when creating an instance of a derived class. This is essential when the base class doesn't have a parameterless constructor or when you need to pass specific values to initialize the base class properly.

By default, C# automatically calls the parameterless constructor of the base class. However, when the base class only has parameterized constructors, you must explicitly specify which constructor to call using the base keyword.

Syntax

Following is the syntax for calling a base class constructor from a derived class −

public class DerivedClass : BaseClass {
   public DerivedClass(parameters) : base(arguments) {
      // derived class constructor body
   }
}

The base(arguments) call happens before the derived class constructor body executes.

Constructor Call Order Base Constructor base(args) Executes First Derived Constructor constructor body Executes Second When creating: new DerivedClass(args) 1. Base constructor runs with passed arguments 2. Derived constructor body executes

Using Base Constructor with Parameters

Example

using System;

class DemoBase {
   public DemoBase(int firstNumber, int secondNumber, int thirdNumber) {
      Console.WriteLine("Base class Constructor");
      Console.WriteLine($"{firstNumber} {secondNumber} {thirdNumber}");
   }
}

class Demo : DemoBase {
   public Demo(int firstNumber, int secondNumber, int thirdNumber) : base(firstNumber, secondNumber, thirdNumber) {
      Console.WriteLine("Derived class Constructor");
      Console.WriteLine($"{firstNumber} {secondNumber} {thirdNumber}");
   }
}

class Program {
   static void Main() {
      Demo obj = new Demo(1, 2, 3);
   }
}

The output of the above code is −

Base class Constructor
1 2 3
Derived class Constructor
1 2 3

Using Multiple Base Constructors

A base class can have multiple constructors, and the derived class can choose which one to call −

Example

using System;

class Vehicle {
   public string Brand { get; set; }
   public int Year { get; set; }

   public Vehicle(string brand) {
      Brand = brand;
      Console.WriteLine($"Vehicle constructor: {brand}");
   }

   public Vehicle(string brand, int year) : this(brand) {
      Year = year;
      Console.WriteLine($"Vehicle constructor with year: {year}");
   }
}

class Car : Vehicle {
   public int Doors { get; set; }

   public Car(string brand, int doors) : base(brand) {
      Doors = doors;
      Console.WriteLine($"Car constructor: {doors} doors");
   }

   public Car(string brand, int year, int doors) : base(brand, year) {
      Doors = doors;
      Console.WriteLine($"Car constructor: {doors} doors");
   }
}

class Program {
   static void Main() {
      Console.WriteLine("Creating Car with brand and doors:");
      Car car1 = new Car("Toyota", 4);
      
      Console.WriteLine("\nCreating Car with brand, year, and doors:");
      Car car2 = new Car("Honda", 2022, 2);
   }
}

The output of the above code is −

Creating Car with brand and doors:
Vehicle constructor: Toyota
Car constructor: 4 doors

Creating Car with brand, year, and doors:
Vehicle constructor: Honda
Vehicle constructor with year: 2022
Car constructor: 2 doors

Default Constructor Behavior

If no base call is specified, C# automatically calls the parameterless constructor of the base class −

Example

using System;

class Animal {
   public Animal() {
      Console.WriteLine("Animal default constructor called");
   }
}

class Dog : Animal {
   public Dog() {
      // Implicit call to base() happens here
      Console.WriteLine("Dog constructor called");
   }
}

class Program {
   static void Main() {
      Dog dog = new Dog();
   }
}

The output of the above code is −

Animal default constructor called
Dog constructor called

Conclusion

The base keyword allows derived classes to explicitly call specific constructors from their parent class. This ensures proper initialization of base class members and gives you control over which base constructor executes, especially when the base class has multiple constructor overloads.

Updated on: 2026-03-17T07:04:36+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements