agentleFS
martincjarvismartincjarvis/development-skills/skills/modern-csharp-coding-standards/SKILL.md

modern-csharp-coding-standards

Skill4 starsChanged 10 months agoMITView on GitHub

Use this skill whenever writing or reviewing C# code to ensure use of modern language features, high performance, and robust API design.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

10 rules. About 0 are common, 3 are distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use of mutable classes where record or readonly record struct is appropriate.

    line 25
  2. Distinctive:

    Ensure all async calls are awaited and tokens passed.

    line 48
  3. Very specific:

    Use Span<T> for buffer/string manipulation where performance is key.

    line 49
  4. Distinctive:

    Run static analysis and linters to ensure compliance.

    line 51
  5. Distinctive:

    Documentation: Document significant architectural patterns (e.g., Value Objects) in an ADR.

    line 54
  6. Very specific:

    Use record and readonly record struct for immutable domain modeling.

    line 61
  7. Very specific:

    Prefer pattern matching over nested if/else branches.

    line 62
  8. Very specific:

    Use Span<T> and Memory<T> for high-performance parsing and buffers.

    line 64
  9. Very specific:

    Validation: Ensure Nullable is enabled in .csproj and zero warnings exist.

    line 71
  10. Very specific:

    Validation: Verify CancellationToken is passed to all downstream async calls.

    line 72

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Modern C# Coding Standards

Intent

Enforce the use of modern C# (12+) features to improve code safety, readability, and performance. This includes a preference for immutability, functional-style patterns, and zero-allocation techniques.


When to Use

  • Writing any new C# code (Records, Pattern matching, Primary constructors).
  • Refactoring existing C# code to improve maintainability or performance.
  • Designing domain models (Value Objects, Entities).
  • Implementing high-performance or async-heavy paths.

Precondition Failure Signal

  • Use of mutable classes where record or readonly record struct is appropriate.
  • Deep inheritance hierarchies instead of composition and interfaces.
  • Manual null checks instead of Nullable Reference Types and pattern matching.
  • Blocking on async code (.Result, .Wait()) or missing CancellationToken.
  • Excessive allocations in performance-critical paths (not using Span<T>).

Postcondition Success Signal

  • Domain models use record (DTOs/Entities) and readonly record struct (Value Objects).
  • Code leverages switch expressions and modern pattern matching.
  • Nullable Reference Types are enabled and warnings addressed.
  • Async methods are "async all the way" and respect CancellationToken.
  • High-performance paths use Span<T>, Memory<T>, or ArrayPool<T>.

Process

  1. Source Review: Inspect the code for outdated patterns (mutability, inheritance, blocking async).
  2. Implementation:
    • Refactor to use modern C# features (e.g., convert class to record).
    • Ensure all async calls are awaited and tokens passed.
    • Use Span<T> for buffer/string manipulation where performance is key.
  3. Verification:
    • Run static analysis and linters to ensure compliance.
    • Execute tests to verify behaviour remains unchanged.
    • (Optional) Use benchmarks to demonstrate performance improvements for zero-allocation changes.
  4. Documentation: Document significant architectural patterns (e.g., Value Objects) in an ADR.
  5. Review: Tech Lead reviews for adherence to the "Modern C#" spirit.

Key Practices

  • Use record and readonly record struct for immutable domain modeling.
  • Prefer pattern matching over nested if/else branches.
  • Pass CancellationToken through async call chains.
  • Use Span<T> and Memory<T> for high-performance parsing and buffers.

Example Test / Validation

  • Refactor: Convert a mutable DTO class to a positional record.
  • Validation: Ensure Nullable is enabled in .csproj and zero warnings exist.
  • Validation: Verify CancellationToken is passed to all downstream async calls.

Common Red Flags / Guardrail Violations

  • "I'm used to the old way" (avoiding records/patterns).
  • Ignoring nullable warnings by overusing !.
  • Creating BaseEntity or BaseService hierarchies.
  • Using AutoMapper (prefer Mapperly for compile-time safety).

Recommended Review Personas

  • Tech Lead – validates modern pattern usage and architectural alignment.
  • Software Engineer – validates implementation details and performance.

Skill Priority

P2 – Consistency & Governance
(Escalate to P1 for performance-critical or security-related code.)


Conflict Resolution Rules

  • Modern C# standards override legacy patterns unless explicitly constrained by framework/interop requirements.
  • Immutability is the default; mutability must be justified and scoped.

Conceptual Dependencies

  • quality-gate-enforcement
  • automated-standards-enforcement

Classification

Governance Core


Notes

See skills/modern-csharp-coding-standards/references/reference.md for focused examples and patterns.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.