<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms" -->

---
title: Why I Switched to Primary Constructors for DI in C#
description: C# 12 primary constructors can significantly reduce boilerplate in DI service classes by eliminating field declarations, constructor bodies, and assignments....
canonical: https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Why I Switched to Primary Constructors for DI in C# | daily.dev
og:description: C# 12 primary constructors can significantly reduce boilerplate in DI service classes by eliminating field declarations, constructor bodies, and assignments....
og:url: https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms
og:image: https://api.daily.dev/og/posts/cTodIIjmS.png
og:image:alt: Why I Switched to Primary Constructors for DI in C#
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Why I Switched to Primary Constructors for DI in C#

**[Milan Jovanović](https://daily.dev/sources/milanjovanovic)** · 6 min read · 53 upvotes · 4 comments

## Summary

C# 12 primary constructors can significantly reduce boilerplate in DI service classes by eliminating field declarations, constructor bodies, and assignments. The author shares their experience switching to primary constructors for ASP.NET Core services, explaining the key pitfall: parameters are captured as mutable variables, not readonly fields, meaning accidental reassignment compiles without warning. The recommended approach is to use primary constructors freely in DI service classes where accidental reassignment is unlikely, but fall back to traditional constructors for validation-heavy types, multiple constructor overloads, or classes with too many dependencies.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.milanjovanovic.tech/blog/why-i-switched-to-primary-constructors-for-di-in-csharp>

## Community discussion

Top comments from developers on daily.dev.

**@stuartgreig** · 3 upvotes

> I love Primary constructors they're so much cleaner, this is a great article about why you should use them

**@herob** · 1 upvotes

> In neovim I had with easy-dotnet suggestion to just this type of refactoring
>
> Roslyn feature would that be?

**@barion** · 0 upvotes

> there is a middle ground: use primary constructors but assign the parameters to readonly fields yourself, in this case no field will be generated (there is a warning if you use the parameter like a field but i recommend naming the parameter and the field identically)

## Similar posts on daily.dev

- [Primary constructors are more than syntactic sugar](https://daily.dev/posts/primary-constructors-are-more-than-syntactic-sugar-15ikh5h1i) · We Are .NET · 3 upvotes · 0 comments
- [12 Rules for Dependency Injection in ASP.NET Core](https://daily.dev/posts/12-rules-for-dependency-injection-in-asp-net-core-nirfrs5ef) · We Are .NET · 3 upvotes · 1 comments
- [\[RFC\] Primary Constructors](https://daily.dev/posts/rfc-primary-constructors-iwpmyko6c) · externals.io · 0 upvotes · 0 comments
- [Primary Constructors](https://daily.dev/posts/primary-constructors-iurbp2lbp) · externals.io · 0 upvotes · 0 comments

---

Tags: [#c#](https://daily.dev/tags/c#), [#dependency-injection](https://daily.dev/tags/dependency-injection), [#aspnetcore](https://daily.dev/tags/aspnetcore), [#domain-driven-design](https://daily.dev/tags/domain-driven-design)

[View this post on daily.dev](https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Why I Switched to Primary Constructors for DI in C#","url":"https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms"},"datePublished":"2026-04-17T23:03:12.355Z","dateModified":"2026-04-17T23:03:38.878Z","description":"C# 12 primary constructors can significantly reduce boilerplate in DI service classes by eliminating field declarations, constructor bodies, and assignments....","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b010556d502a956d76c0a44d95720224?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b010556d502a956d76c0a44d95720224?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Milan Jovanović","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Milan Jovanović","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/78d8abfb692746538410dee47f93e566","url":"https://daily.dev/sources/milanjovanovic"},"commentCount":4,"discussionUrl":"https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":53},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":4}],"keywords":"c#,dependency-injection,aspnetcore,domain-driven-design","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Milan Jovanović","item":"https://daily.dev/sources/milanjovanovic"},{"@type":"ListItem","position":3,"name":"Why I Switched to Primary Constructors for DI in C#"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/why-i-switched-to-primary-constructors-for-di-in-c--ctodiijms","comment":[{"@type":"Comment","text":"I love Primary constructors they’re so much cleaner, this is a great article about why you should use them","datePublished":"2026-04-18T09:07:37.277Z","url":"https://daily.dev/posts/cTodIIjmS#c-q7hLIm16B","author":{"@type":"Person","name":"Stuart Greig","url":"https://daily.dev/stuartgreig","image":"https://media.daily.dev/image/upload/s--FoVqUWgr--/f_auto/v1772633885/avatars/avatar_V6TOuZS8XXplYvc5hNLgE?_a=BAMAMiiu0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3}},{"@type":"Comment","text":"In neovim I had with easy-dotnet suggestion to just this type of refactoring\nRoslyn feature would that be?","datePublished":"2026-04-19T07:51:25.547Z","url":"https://daily.dev/posts/cTodIIjmS#c-EX4cYfJ5h","author":{"@type":"Person","name":"Herob","url":"https://daily.dev/herob","image":"https://lh3.googleusercontent.com/a/ACg8ocJlpO717SD8qhLLeJ5BVinourR043wcCMUzPb-9l_E-E5HXug=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"there is a middle ground: use primary constructors but assign the parameters to readonly fields yourself, in this case no field will be generated (there is a warning if you use the parameter like a field but i recommend naming the parameter and the field identically)","datePublished":"2026-04-25T07:38:39.694Z","url":"https://daily.dev/posts/cTodIIjmS#c-xbWWj4vA0","author":{"@type":"Person","name":"Barion","url":"https://daily.dev/barion","image":"https://avatars.githubusercontent.com/u/76686287?v=4"}}]}
```

