modern-csharp-coding-standards

Write modern C# code using records, pattern matching, Span<T>, and async best practices.

Updated Aug 10, 2025
One-click install
npx skills add https://github.com/afonsoft/ai-studio-workspace --skill modern-csharp-coding-standards-afonsoft
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: modern-csharp-coding-standards
Source: https://github.com/afonsoft/ai-studio-workspace/tree/main/agents/skills/modern-csharp-coding-standards
Command: npx skills add https://github.com/afonsoft/ai-studio-workspace --skill modern-csharp-coding-standards-afonsoft

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Inconsistent C# codebases suffer from mutable DTOs, deep inheritance hierarchies, reflection-based mapping bugs, and allocation-heavy code that hurts performance. This Skill provides concrete coding standards and patterns for writing modern, type-safe, high-performance C# 12+ code. ## Core Features & Use Cases - Immutable Data Modeling: Use record types for DTOs and readonly record struct value objects with validation, factory methods, and no implicit conversions. - Performance Patterns: Apply Span<T>, Memory<T>, ArrayPool<T>, and async/await with CancellationToken for zero-allocation, high-throughput code paths. - Error Handling & API Design: Implement the Result<T, TError> pattern for expected errors, composition over inheritance, and explicit mapping methods instead of AutoMapper. - Use Case: When refactoring a legacy order-processing service, apply these standards to replace mutable classes with records, eliminate AutoMapper runtime failures with explicit mapping extensions, and convert byte[] buffer code to Span-based zero-allocation parsing. ## Quick Start Use the modern-csharp-coding-standards skill to review my OrderService class and refactor it to use records, value objects, and proper async patterns.

Frequently Asked Questions about modern-csharp-coding-standards

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write immutable DTOs in modern C#?▼

Use record types with positional parameters, such as public record CustomerDto(string Id, string Name). Records provide value-based equality, immutability by default, and work seamlessly with pattern matching and with-expressions for creating modified copies.

Should I use record class or record struct for value objects?▼

Use readonly record struct for value objects like OrderId or Money to get value semantics, stack allocation, and no GC pressure. Use record class for entities, aggregates, and DTOs with multiple properties where reference semantics are appropriate.

Why avoid AutoMapper in C# projects?▼

AutoMapper uses reflection-based mapping that fails silently at runtime when property names or types mismatch. Explicit mapping extension methods provide compile-time safety, work with IDE refactoring, are debuggable, and have no reflection overhead.

When should I use Span<T> versus Memory<T> in C#?▼

Use Span<T> for synchronous zero-allocation operations like parsing and slicing buffers. Use Memory<T> for async operations since Span cannot cross await boundaries. For large temporary buffers over 1KB, rent from ArrayPool<T> to reduce GC pressure.

When should I use Result type instead of exceptions in C#?▼

Use Result<T, TError> for expected errors like validation failures, business rule violations, and not-found conditions. Reserve exceptions for unexpected errors such as network failures, system errors, and programming bugs.

Does this approach work with nullable reference types enabled?▼

Yes, the standards require enabling nullable reference types in the .csproj file. They include patterns for explicit nullability, null-conditional pattern matching, and ArgumentNullException.ThrowIfNull guard clauses available in C# 11 and later.