C# Reference ยท Classes and Object-Oriented Concepts
Primary constructors
Reference for C# 12 primary constructors on classes and structs: captured parameters, initialization, validation, differences from records and dependency injection use.
A primary constructor puts the constructor's parameter list directly on the type declaration. Since C# 12 this works for classes and structs (records have had it since C# 9). The parameters are in scope for the whole type body: field and property initializers, methods and accessors.
The important rule: for a class or struct, a primary-constructor parameter is not a property or field. It is just a parameter that the compiler captures into a hidden field if some member uses it after construction. Records are different: their parameters also become public properties.
public class Greeter(string greeting) { public string Greet(string name) => $"{greeting}, {name}!"; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
class Name(Type1 p1, Type2 p2) : BaseClass(p1) { public Type1 Prop { get; } = p1; // initializer uses the parameter void M() => Use(p2); // p2 is captured } struct Pair(int a, int b); // interfaces and enums cannot have primary constructors
| Part | Description |
|---|---|
| Parameter list | Same rules as method parameters, including default values and params. |
| Base call | : Base(args) after the parameter list passes arguments to the base constructor. |
| Other constructors | Any additional constructor must chain to the primary one with : this(...). |
Errors: using a parameter name through an instance (obj.x) does not compile because there is no member. A non-chaining extra constructor is a compile error.
Usage notes
- Capture happens only when needed. If a parameter is used only in initializers, no field is created. If any method uses it, the compiler adds a private field.- Captured parameters are mutable. Unlike record properties,
start++inside a method modifies the captured copy. Copy the value into areadonlyfield or get-only property when you need immutability. - Don't capture and also copy. If you initialize a property from a parameter and also use that parameter in methods, you hold the value twice. Use the property in methods instead of the parameter.
- Validate in initializers. A primary constructor has no body, so check arguments in a field or property initializer (
?? throw ...) or with a helper call. - Great for dependency injection.
class OrderService(IRepo repo, ILogger<OrderService> log)removes the field-and-assignment boilerplate in ASP.NET Core services. - Records already do this and more: public properties, value equality,
ToStringandwith. Pick a record for data; pick a class primary constructor for behavior-focused types. - Structs: parameters are captured the same way. A
readonly structwith a primary constructor is fine as long as no member modifies a parameter. - Inheritance: a derived class passes arguments to the base class in its declaration:
class Dog(string name) : Animal(name).