---
applyTo: "**/*.cs"
description: C# conventions
---

# C# (.NET 10)

- Nullable reference types are enabled. Model optionality with `?` and handle it; don't use `!` to silence warnings.
- File-scoped namespaces, primary constructors for DI-only classes, `record` for immutable data, collection expressions (`[]`).
- Async I/O everywhere: `async Task`/`ValueTask`, accept and pass `CancellationToken`, never `.Result`/`.Wait()`/`async void` (except event handlers).
- Use `ILogger<T>` with message templates (`"Processing {OrderId}"`), not interpolated strings.
- Guard clauses with `ArgumentNullException.ThrowIfNull(x)` / `ArgumentException.ThrowIfNullOrEmpty(s)`.
- Prefer pattern matching (`is`, `switch` expressions) over casts and long if/else chains.
- LINQ for clarity, loops for hot paths; materialize EF queries once (`ToListAsync`).
- `IDisposable`/`IAsyncDisposable` resources in `using`/`await using`.
- Configuration via the options pattern with validation (`ValidateOnStart`).
- Public APIs get XML doc comments.

## Check
`dotnet build -warnaserror && dotnet test && dotnet format --verify-no-changes`.
