Writes and improves idiomatic, safe, maintainable C# with attention to language version, nullability, ownership, API design, and failure behavior. Use for general C# implementation questions; do not use for focused ASP.NET, EF Core, async, security, or review workflows (use the relevant specialist skill).
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Csharp Best Practices?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/k4ndev-csharp-best-practices)
---
name: csharp-best-practices
description: Writes and improves idiomatic, safe, maintainable C# with attention to language version, nullability, ownership, API design, and failure behavior. Use for general C# implementation questions; do not use for focused ASP.NET, EF Core, async, security, or review workflows (use the relevant specialist skill).
---
# C# Best Practices
## Purpose
Make everyday C# correct, readable, maintainable, and consistent with the target project's actual compiler and runtime.
## Scope
Language-level implementation and small refactors: types, nullability, collections, exceptions, resource ownership, formatting, and public method design.
## When to use
- A user asks how to implement a general C# feature or improve ordinary C# code.
- The question spans language design but not a specific framework subsystem.
## Do not use for
- ASP.NET endpoints/middleware, EF queries/migrations, security audit, or concurrency diagnosis: use the specialist skill.
- Reviewing a proposed change as a whole: use `dotnet-code-review`.
## Required context
Check `TargetFramework`, `LangVersion`, `Nullable`, analyzers, existing conventions, and whether the value is public API. If missing, state assumptions and use only syntax supported by the known target.
## Workflow
1. Restate the behavior and constraints; distinguish value semantics from identity and ownership.
2. Read surrounding call sites before changing a public signature or exception contract.
3. Keep null handling at clear boundaries; avoid `!` unless an invariant is proved and documented.
4. Choose appropriate types: `decimal` for decimal currency arithmetic, explicit `StringComparison` for machine comparisons, immutable collections only where mutation is not part of the contract.
5. Dispose resources the code owns; distinguish owned from injected/caller-owned resources.
6. Preserve cancellation and exception meaning; do not catch and silently discard failures.
7. Add behavior-focused tests for changed logic and run the narrowest applicable build/test command when requested or appropriate.
## Decision rules
- Prefer the simplest construct that expresses intent; use records only when value equality is desired.
- Avoid public mutable fields, hidden service locators, ambient culture for protocol data, mutable global state, and blanket suppressions.
- Do not add abstractions for hypothetical reuse; use the focused skill when framework lifetime or security semantics dominate.
## Expected output format
Give a brief recommendation, a minimal compilable example, version assumptions, why the alternative is safer, and tests/commands actually performed. For edits, summarize touched files and behavioral changes.
## Common mistakes
- Using `double` for money, current culture for machine formats, `async void` for library APIs, or null-forgiving operators to hide unknown state.
- Disposing an injected service owned by DI, swallowing exceptions, or using new language features unsupported by the project.
## Edge cases
- Nullable oblivious legacy projects: avoid pretending annotations prove runtime safety.
- `DateTime`/time zones: establish UTC/offset semantics; prefer `DateTimeOffset` for instants where that matches domain needs.
- Public API: check binary/source compatibility and default values before changing signatures.
## Examples
See [C# examples](examples.md) for culture-safe parsing and resource ownership.
## Test scenarios
- Positive: “Parse a decimal amount from an invariant CSV field without current-culture bugs.”
- Negative: “Review this controller's authorization” routes to `security-audit` / `aspnetcore-patterns`.
- Boundary: “Should this DTO be a record?” uses this skill; if reviewing a diff, `dotnet-code-review` owns the review.
## Related skills
`dotnet-code-review`, `async-concurrency`, `testing-quality`, `security-audit`. Prefer the domain skill where framework behavior is decisive.