What is a Primary Constructor?
Introduced in C# 12, Primary Constructors allow you to declare a constructor and its parameters directly in the class or struct definition. This removes the need for boilerplate constructor bodies and private fields.
csharp
How they Work
Wait! The parameters of a primary constructor are in scope for the entire class. You can use them to initialize properties, fields, or even use them directly in methods.
csharp
Why use them?
- Readability: Makes your class definitions much shorter and cleaner.
- Productivity: You don't have to manually map parameters to private fields.
- Dependency Injection: They are perfect for injecting services into controllers or repositories.
Summary
| Term | Description |
|---|---|
| Primary | The constructor defined on the class header |
| Scope | Parameters are available throughout the class body |
| Capture | The compiler automatically stores parameters if needed |
| Boilerplate | Dramatically reduced |
| Best For | Data-centric classes, DI services, and internal helpers |
| Logic | "One place for definition and initialization" |
| Safety | Still fully type-safe and checked at compile-time |
| Modern | Inspired by Scala, Kotlin, and C# Records |
| Standard | Now the recommended way to handle DI in ASP.NET Core |
| Limitation | Unlike Records, these don't generate properties automatically |