Urgent.News

What's breaking now, across thousands of outlets.

Tech

Modern C# 12: Refactoring with Primary Constructors and XML Docs

Simplifying Dependency Injection with C# 12 Primary Constructors C# development has evolved significantly with .NET 8 and C# 12. Historically, dependency injection required declaring private read-only fields ( private readonly ), defining an explicit constructor, and manually assigning each parameter to those fields. This pattern produced considerable boilerplate code, making domain and service…

C# development has seen substantial changes with the release of .NET 8 and C# 12, streamlining dependency injection through a new feature called Primary Constructors. This evolution has simplified the creation of dependency-injected classes, eliminating the need for lengthy boilerplate code. Historically, developers had to declare private read-only fields, define an explicit constructor, and assign each parameter manually, leading to cumbersome code and difficulties in maintaining domain and service classes.

With C# 12's primary constructors, developers can now declare parameters directly within the class definition, streamlining the process while maintaining clarity and type safety.

In legacy C# code before the advent of primary constructors, a payment processing service class necessitated numerous lines to set up dependencies. The class was annotated with XML documentation tags to provide clarity on parameters, such as the primary payment provider and the audit logging service. Despite the effectiveness of this approach, it was often verbose and required significant boilerplate code.

With the introduction of primary constructors in C# 12, the PaymentProcessor class underwent a significant transformation. The class now declares dependencies directly in its definition, eliminating the need for a separate constructor and parameter assignment. This not only reduces code clutter but also enhances readability and understanding of the class's dependencies at a glance.

The integration of these modern features with .NET Analyzers and EditorConfig ensures uniformity across development teams. By configuring static analysis rules in the .editorconfig file, developers are prompted to refactor toward primary constructors, maintaining a consistent coding style. Furthermore, automated documentation tools, such as the Code Formatter & Auto-Doc AI, parse primary constructor parameters to generate necessary XML documentation tags.

This automation guarantees compliance with coding standards, such as StyleCop (SA1600), and aligns with official Microsoft guidelines, all without manual intervention.

The adoption of primary constructors in C# 12 thus represents a paradigm shift towards cleaner, more efficient, and maintainable codebases. By reducing boilerplate, improving code clarity, and automating documentation processes, developers can focus more on business logic rather than cumbersome setup procedures. This modern approach not only aligns with best practices but also enhances the overall quality and readability of C# applications.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

ERR_PNPM_OUTDATED_LOCKFILE in GitHub Actions

ERR_PNPM_OUTDATED_LOCKFILE in GitHub Actions means pnpm ran a frozen install, compared pnpm-lock.yaml against every package.json it can see, and found a difference it is not allowed to write away.

  • ERRPNPMOUTDATEDLOCKFILE error caused by discrepancies between package.json and pnpm-lock.yaml
  • Regenerate lockfile using same pnpm version in package.json and commit changes
  • Ensure all dependencies are up-to-date and commit install settings alongside manifest changes

Docker OCI runtime exec failed

Docker OCI runtime exec failed comes from docker exec , not from starting a container, so to fix docker exec failures you look at the command you asked for and at the state of the container, never at…

  • Docker OCI runtime exec failed indicates command execution issue inside running container
  • Error may stem from missing executable file in $PATH or non-running container
  • Ensure binary inclusion, container status, and correct command syntax to resolve

Can RTX 3090 24GB really run Qwen4: The posted numbers don't tell.

By Nokka (นก-กา) | November 10, 2026 This article was written by AI (deepseek-v4.1-flash · Model provider: ollama-cloud) via Hermes Agent under human control and quality review - Nokka (นก-กา) posted…

  • RTX 3090 with 24GB VRAM can run Qwen3.8-27B model
  • Qwen4-27B model does not exist in official organization
  • 24GB limit is significant bottleneck for newer models

More from Sunday 15 November →