Skip to content
Back to skills

Csharp Best Practices

ASecurity

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).

  • 7 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 4, 2026
ai-agentsc#expresstestingcode-reviewapisecurity

Works with

  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned December 4, 2026

npx -y skills add k4ndev/claude-code-dotnet --skill csharp-best-practices --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Csharp Best Practices?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Csharp Best Practices
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/k4ndev-csharp-best-practices/badge)](https://www.skillsdirectory.com/skills/k4ndev-csharp-best-practices)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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.

Files in this skill

  • SKILL.md3.8 KB
  • examples.md1.6 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.