Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~3 min readC#
#C##Primary Constructors#Class Design#Dependency Injection
Diagram illustrating C# primary constructor class syntax with parameters and class body.

Primary constructors in C# let you declare constructor parameters directly in the class declaration. Instead of writing a separate constructor method, you add parameters after the class name. The compiler generates a constructor from that declaration and makes the parameters available in instance members throughout the class body. That reduces boilerplate for simple initialization patterns.

csharp
public class Person(string name, int age) { public string Name { get; } = name; public int Age { get; } = age; }

Here, name and age are primary constructor parameters. They are in scope in instance members throughout the class body, so you can use them in property initializers, field initializers, or base constructor calls.

How Primary Constructors Differ from Traditional Constructors

With a traditional constructor, you explicitly define a constructor method and assign fields or properties. For a class with several dependencies, that often leads to repetitive code.

csharp
public class OrderService { private readonly IOrderRepository _repository; private readonly ILogger<OrderService> _logger; public OrderService(IOrderRepository repository, ILogger<OrderService> logger) { _repository = repository; _logger = logger; } }

With a primary constructor, the same class becomes:

csharp
public class OrderService(IOrderRepository repository, ILogger<OrderService> logger) { private readonly IOrderRepository _repository = repository; private readonly ILogger<OrderService> _logger = logger; }

The parameters are in scope for field initializers and property initializers. This is particularly useful for dependency injection, where the constructor mainly assigns dependencies to private fields.

Capturing Primary Constructor Parameters

Primary constructor parameters are not automatically exposed as public properties. This is a key difference from record types, where positional parameters are automatically turned into properties.

If you need a named field or property, assign the parameter explicitly. For example:

csharp
public class Point(double x, double y) { public double X { get; } = x; public double Y { get; } = y; }

The Point class stores x and y in properties so they remain available as named members. If you only use a primary constructor parameter in initializers, the compiler does not need to keep a backing field for it. If you use it in an instance method, the compiler may capture it into a hidden private field, but that field is not publicly exposed. Use an explicit property or field when the value should be part of the class's API.

Using Primary Constructors with Base Classes

Primary constructor parameters can be passed to a base class constructor. This is done in the class declaration using the base keyword.

csharp
public class DerivedClass(int value) : BaseClass(value) { // ... }

This works because the primary constructor parameters are in scope when the base class initializer is evaluated. This can reduce the need for a separate constructor that forwards arguments.

Dependency Injection with Primary Constructors

Primary constructors are a natural fit for dependency injection because the dependencies are visible directly in the class declaration. Many DI containers treat a primary constructor like a normal constructor, so no special configuration is required.

If you need to validate dependencies or perform logic before assigning fields, you cannot put that logic in the parameter list. You can use a field or property initializer that calls a method, or fall back to a traditional constructor for more complex setup.

Limitations and Considerations

Primary constructors have some limitations:

  • They are not automatically stored as public members; assign parameters to fields or properties when they need to be part of the class's API.
  • You cannot declare an explicit constructor with the same signature as the primary constructor.
  • Parameters are available to instance members throughout the class body, but they are not publicly accessible members of the class.
  • Additional constructors must chain to the primary constructor with this(...) if you need overloads.
  • They require C# 12 or later.

For simple classes that store parameters, primary constructors reduce boilerplate. For classes with complex initialization logic, a traditional constructor may be clearer.

When to Use Primary Constructors

Use a primary constructor when:

  • The class is simple and only needs to store constructor parameters.
  • You are using dependency injection and the constructor only assigns dependencies.
  • You want to reduce repetitive code for small data-holding classes.

Avoid or reconsider primary constructors when:

  • You need to perform validation or complex logic in the constructor.
  • You need multiple constructor overloads that do not fit the primary constructor's parameter list.
  • You are targeting an older C# version.

Primary constructors are a compile-time feature; the compiler emits an ordinary constructor, so there is no separate runtime dispatch layer. The decision to use them should be based on code clarity and maintainability, not on runtime behavior.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.