Skip to main content

How to Inherit from a Class and Call the Base Constructor in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

When one kind of thing is a special case of another, like a dog is an animal, a C# class can inherit from another: class Dog : Animal. The dog gets everything the animal has, and its constructor hands the values the animal needs to the animal's constructor with : base(...).

Syntax​

Syntax
class <Derived> : <Base>
{
public <Derived>(<parameters>) : base(<arguments>)
{
<code specific to Derived>
}
}
PartWhat it does
class <Derived> : <Base>Declares that <Derived> inherits from <Base>, and gets its members.
: base(<arguments>)Calls the base class's constructor with these arguments before the derived constructor's body runs.
<code specific to Derived>Whatever the derived class adds. It may be empty.

Microsoft's inheritance guide: "Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class." The base keyword reference lists, among its uses: "Specify which base-class constructor to call when creating instances of the derived class."

The Example​

An Animal class stores a name and can describe itself. A Dog class inherits from it, passes its name to Animal's constructor, and adds a Speak method of its own.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which creates a Dog named Rex and prints Describe() and Speak(), where Animal has a constructor that sets Name and a Describe method, and Dog inherits from Animal, calls base(name) in its constructor and adds Speak.

main.cs
var dog = new Dog("Rex");
Console.WriteLine(dog.Describe());
Console.WriteLine(dog.Speak());

class Animal
{
public Animal(string name)
{
Name = name;
}

public string Name { get; }

public string Describe() => $"{Name} is an animal.";
}

class Dog : Animal
{
public Dog(string name) : base(name)
{
}

public string Speak() => $"{Name} says woof.";
}

Dog never declares Name or Describe: it inherits both from Animal. Its constructor body is empty because : base(name) already did the work.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Rex is an animal.
Rex says woof.

A zsh terminal where dotnet run main.cs prints Rex is an animal. and Rex says woof.

Describe came from Animal, Speak from Dog, and both saw the name Rex that : base(name) passed to Animal. The command is the same in zsh, bash and PowerShell.

What Happens Without : base(...)​

Leave out the call to the base constructor:

main.cs
var dog = new Dog("Rex");
Console.WriteLine(dog.Name);

class Animal
{
public Animal(string name)
{
Name = name;
}

public string Name { get; }
}

class Dog : Animal
{
public Dog(string name)
{
}
}
Output
/Users/you/demo/main.cs(16,12): error CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Animal.Animal(string)'

The build failed. Fix the build errors and run again.

Without : base(...), C# tries to call Animal's constructor with no arguments, and there isn't one. Microsoft's constructors guide: "If a base class doesn't offer a parameterless constructor, the derived class must make an explicit call to a base constructor by using base."

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out : base(...) when the base class has no parameterless constructorerror CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Animal.Animal(string)'Add : base(name) to the derived constructor
Expecting new Dog("Rex") to work without a Dog constructorConstructors aren't inherited, so Dog has no constructor that takes a nameDeclare one and pass the name on with base
Copying the base class's fields into the derived classTwo copies of the same dataUse the inherited members

Troubleshooting​

SymptomLikely causeFix
CS7036 pointing at the derived class's constructorThe base constructor needs arguments that aren't passedAdd : base(<arguments>)
A member of the base class isn't visible in the derived classIt's privateMake it protected or public

FAQ​

Are constructors inherited?​

No. The inheritance guide: "When you define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and finalizers." That's why Dog declares its own constructor.

Can a class inherit from two classes?​

No. The guide: "A derived class can have only one direct base class." It can implement any number of interfaces, though.

Conclusion​

class Dog : Animal gives Dog everything Animal has, and public Dog(string name) : base(name) passes the constructor's value on to Animal's constructor, which runs first. Without that call, a base class that needs arguments stops the build with CS7036.

Additional Resources​