Skip to main content

How to Pass Primary Constructor Parameters to a Base Class in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A class with a primary constructor takes its parameters in the class line itself. When it derives from a class that needs constructor arguments, it passes them in that same line, right after the base class's name.

Syntax​

Syntax
class <Derived>(<type> <parameter>, ...) : <Base>(<arguments>)
{
}
PartWhat it does
<Derived>(<type> <parameter>, ...)The derived class's primary constructor: its parameters
: <Base>The class it derives from
(<arguments>)The arguments for the base class's constructor, which can use the primary constructor's parameters

Microsoft's primary constructors tutorial: "You can invoke the primary constructor for a base class from the primary constructor of derived class." And about its own example: "This expression specifies both the type for the base class and the arguments for the primary constructor."

The Example​

An Animal class takes a name and keeps it in a Name property. A Dog takes a name too, passes it on to Animal, and adds a Speak method that uses Name.

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 calls Speak, with a class Animal whose primary constructor takes name and stores it in a Name property, and a class Dog whose primary constructor takes name and passes it to Animal(name), with a Speak method that prints Name says woof.

main.cs
Dog dog = new("Rex");
dog.Speak();

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

class Dog(string name) : Animal(name)
{
public void Speak() => Console.WriteLine($"{Name} says woof.");
}

Dog(string name) : Animal(name) hands the name Dog received to Animal's constructor.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Rex says woof.

A zsh terminal where dotnet run main.cs prints Rex says woof.

new("Rex") went to Dog's primary constructor, which passed "Rex" to Animal's. Animal stored it in Name, and Speak, in Dog, read it from there.

The command is the same in zsh, bash and PowerShell.

Without the Arguments​

Leave the arguments off the base class:

main.cs
class Dog(string name) : Animal
{
public void Speak() => Console.WriteLine($"{Name} says woof.");
}
Output
/Users/you/demo/main.cs(9,7): error CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Animal.Animal(string)'
/Users/you/demo/main.cs(9,18): warning CS9113: Parameter 'name' is unread.

The build failed. Fix the build errors and run again.
  • CS7036: Animal's only constructor needs a name, and nothing gave it one.
  • CS9113: Dog's name parameter is now used nowhere, so the compiler says it's unread.

Common Mistakes​

MistakeWhat happensDo this instead
: Animal without arguments, when Animal's constructor needs themerror CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Animal.Animal(string)': Animal(name)
A primary constructor parameter that's never usedwarning CS9113: Parameter 'name' is unread.Use it, or remove it
Reading the parameter in Dog's methods as well as passing it onwarning CS9107: Parameter 'string name' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.Read the base class's property, Name, as Speak does

Troubleshooting​

SymptomLikely causeFix
CS7036 on the derived class's lineNo arguments for the base class's constructorAdd them after the base class's name
CS9113 on a primary constructor parameterThe parameter isn't passed on or usedPass it to the base class, or use it
CS9107 on the base class's argumentsThe derived class reads the parameter too, so two copies may be keptRead the base class's property instead

FAQ​

Does the base class need a primary constructor too?​

No. Microsoft's guide to constructor errors: "The base class must have a matching constructor, but it doesn't need a primary constructor."

Can the arguments be expressions?​

Yes. With class Dog(string name) : Animal(name.ToUpperInvariant()), the same program printed REX says woof.: Animal got the name in capitals.

Conclusion​

To pass a primary constructor's parameters to a base class, write the arguments after the base class's name in the class line: class Dog(string name) : Animal(name). Without them, a base class whose constructor needs arguments stops the build with CS7036.

Additional Resources​