C# Primary Constructor Inheritance Explained
Learn how C# primary constructors behave with inheritance: passing parameters to a base class, capturing values, and planning around key limitations.

When C# 12 introduced primary constructors for classes and structs, inheritance immediately raised a practical question: how does a derived class pass arguments to a base class that also declares a primary constructor? The answer is straightforward, but it involves a few rules that are easy to miss. This article explains how C# primary constructor inheritance works, shows examples, and covers the limitations you need to plan around.
Primary Constructor Syntax in Brief
A primary constructor is declared as part of the type declaration. The parameters are in scope throughout the class body, but they are not automatically stored as fields unless you explicitly capture them.
public class Product(string name, decimal price)
{
public string Name { get; } = name;
public decimal Price { get; } = price;
}Here, name and price are used to initialize the properties. The compiler generates a constructor that accepts those parameters. If you do not use a parameter anywhere, the compiler does not store it; the value is available only while that constructor is running.
How Inheritance Works with Primary Constructors
When a derived class has a primary constructor, it can use a : base(...) initializer to call a base constructor. If the base class has an accessible parameterless constructor, that initializer is optional; otherwise the derived class must call a base constructor explicitly.
public class Book(string title, string author, string isbn)
: Product(title, 0m) // base primary constructor call
{
public string Author { get; } = author;
public string Isbn { get; } = isbn;
}The derived class's primary constructor parameters are available in the base initializer, just as constructor parameters are in a traditional constructor. This is the core behavior of C# primary constructor inheritance: you can pass any of the derived class's parameters to the base constructor while those parameters are in scope.
If you do not provide an explicit base call, the compiler tries to call a parameterless base constructor. If the base class has a primary constructor with required parameters and no parameterless constructor, the code will not compile.
Passing Parameters to a Base Primary Constructor
Consider a base class that owns its data:
public class Account(string accountNumber, string ownerName)
{
public string AccountNumber { get; } = accountNumber;
public string OwnerName { get; } = ownerName;
}A derived class for a savings account can pass its own parameters through:
public class SavingsAccount(string accountNumber, string ownerName, decimal interestRate)
: Account(accountNumber, ownerName)
{
public decimal InterestRate { get; } = interestRate;
}The base initializer runs before the body of the derived class. The accountNumber and ownerName parameters are captured by the base class properties, while interestRate is used by the derived class. The compiler does not create an implicit field for interestRate unless an instance member references it; here the property initializer references it, so the value is captured.
Using Primary Constructor Parameters in the Derived Class
Primary constructor parameters are available in property initializers, method bodies, and other instance members of the derived class. They are not stored as fields unless you capture them. If you need to use a parameter later, assign it to a property or field, or reference it in an instance member that can be called after construction.
public class Order(int orderId, decimal amount)
{
public int OrderId { get; } = orderId;
public decimal Amount { get; } = amount;
public string Describe() => $"Order {OrderId} for {Amount:C}";
}In this example, orderId and amount are captured because they are used in property initializers. If they were referenced only inside Describe(), the compiler would also generate fields because that method can be called after construction. The compiler decides whether to store a parameter based on whether it is used in an instance member that may be called after the constructor finishes.
Limitations and Edge Cases
Primary constructors have specific constraints that affect inheritance scenarios.
- You cannot have both a primary constructor and a traditional constructor with the same signature. The primary constructor is the only constructor that takes the declared parameters.
- Primary constructor parameters cannot be used in static members or static fields. They are instance-scoped.
- If a derived class has a primary constructor, any additional constructors must delegate to it with
this(...); they cannot call a different base constructor directly. - A primary constructor has one fixed
baseinitializer. You cannot choose among base constructors conditionally.
A common mistake is trying to use a primary constructor parameter in a static property:
public class Example(int value)
{
public static int StaticValue => value; // Compiler error
}This fails because value is not available in a static context. The parameter exists only for the instance being constructed.
Another edge case: if the base class declares a primary constructor but has no accessible parameterless constructor, a derived class must pass values through an explicit base call. The compiler will not invent values for that call.
Compatibility and Language Version Requirements
Primary constructors are a C# 12 compiler feature. They do not require runtime support, so they work on any .NET runtime the compiler can target, including .NET Framework, as long as you use a compiler that supports C# 12. In practice, use Visual Studio 17.8 or later, or the .NET 8 SDK.
C# 12 is the default for projects that target .NET 8. For older target frameworks, or if the default language version is lower than 12, set <LangVersion> to 12 in the project file:
<PropertyGroup>
<LangVersion>12</LangVersion>
</PropertyGroup>Targeting an older framework does not prevent the compiler from emitting the constructor code. You can use primary constructors as long as the compiler supports C# 12, but verify the build environment before adopting them in shared projects.
When to Prefer Primary Constructors Over Traditional Constructors
Primary constructors shine when a type's main purpose is to hold data and the constructor simply assigns that data to properties. They reduce boilerplate and make the class signature explicit. Use them when:
- The class has few dependencies and no complex validation logic in the constructor.
- The parameters map directly to properties or are used only during construction.
- You want to keep the type declaration concise, especially for DTOs or value objects.
Avoid primary constructors when:
- You need constructor overloads that are not all routed through a single primary constructor.
- You need to perform complex validation that involves throwing exceptions from a helper method.
- You need to use constructor parameters in static members or in a way that requires them to be stored explicitly.
- You are working with a codebase that targets older language versions and you cannot update the compiler.
For inheritance, primary constructors work well when the derived class also has a straightforward data shape. If the derived class needs different base constructor calls for different overloads, traditional constructors give you more flexibility because each overload can choose its own base initializer.
The decision ultimately comes down to whether the primary constructor's constraint of a single base initializer and a single constructor entry point is acceptable. For many types, it is. For types that need overloads with independent base calls, stick with traditional constructors.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.