Inheritance in C#: How to Call a Base Class Constructor (Complete Guide)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Is the Base Class Constructor Not Just "Boring Theory"?

\n\n

Imagine you are building a house. You have a foundation (base class) and the house itself (derived class). The foundation is already laid, but for the house to stand strong, you need to properly "connect" it to the foundation. In C#, this "connection" is handled by calling the base class constructor using the base keyword.

\n\n

This is one of those topics that generates thousands of questions on Stack Overflow. Why? Because without understanding this mechanism, your code will either not compile or will behave unpredictably. In this article, we will break down calling a base class constructor in C# from simple to complex: with examples, pitfalls, and real-world scenarios.

\n\n

What will you learn from this article?

\n
    \n
  • How constructor inheritance works in C#.
  • \n
  • When and why you need to explicitly call base().
  • \n
  • How to pass parameters to the base class constructor.
  • \n
  • Typical mistakes and how to avoid them.
  • \n
  • Advanced techniques: constructor chains and overloading.
  • \n
\n\n

1. Basics: What Is a Base Class Constructor?

\n\n

In C#, each class can have one or more constructors. When you create an instance of a derived class, the base class constructor is called first. This ensures that all fields and properties of the "parent" are initialized before the "child" starts working.

\n\n

If you do not explicitly specify which base class constructor to call, the compiler will try to call the default constructor (without parameters). If it doesn't exist, you will get a compilation error.

\n\n

Example 1: Implicit Call (Simple)

\n
public class Animal\n{\n    public string Name { get; set; }\n\n    public Animal()\n    {\n        Name = "Unknown animal";\n        Console.WriteLine("Animal: parameterless constructor");\n    }\n}\n\npublic class Dog : Animal\n{\n    public Dog()\n    {\n        Console.WriteLine("Dog: constructor");\n    }\n}\n\n// Usage\nDog dog = new Dog();\n// Output:\n// Animal: parameterless constructor\n// Dog: constructor
\n\n

Here, Dog implicitly calls Animal(). It works because Animal has a default constructor.

\n\n

2. The Problem: When There Is No Default Constructor

\n\n

Now imagine that the base class requires mandatory parameters. For example, an animal must have a name. If you remove the parameterless constructor, the code will stop compiling.

\n\n

Example 2: Compilation Error

\n
public class Animal\n{\n    public string Name { get; }\n\n    public Animal(string name) // single constructor\n    {\n        Name = name;\n    }\n}\n\npublic class Dog : Animal\n{\n    // Error CS7036: "Animal" does not contain a constructor that takes 0 arguments\n    public Dog() \n    {\n    }\n}
\n\n

The compiler screams: "Hey, I don't know how to initialize Animal without a name!". The solution is to explicitly call the base class constructor using base.

\n\n

3. The base Keyword: Explicit Constructor Call

\n\n

The base keyword in C# is used to access members of the base class. In the context of constructors, it allows you to specify which parent constructor to call and with what arguments.

\n\n

Syntax:

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

The colon and base(arguments) are placed immediately after the constructor signature, before the body.

\n\n

Example 3: Fixing the Error

\n
public class Animal\n{\n    public string Name { get; }\n\n    public Animal(string name)\n    {\n        Name = name;\n        Console.WriteLine($"Animal: created {name}");\n                

Blogs

Book Recommendations