refactor(rules): split C# guidance by topic

This commit is contained in:
fraujulian committed 2026-09-24 21:46:50 +02:00
1 parent d4f45a3320
commit 27db155b85
10 files changed
+118 -117

No files matched your search

@@ -0,0 +1,13 @@
# C# Async and Concurrency
Apply these rules when adding or changing asynchronous APIs, background work, or concurrent processing.
* Use asynchronous APIs for genuinely asynchronous I/O; do not wrap synchronous work in unnecessary tasks.
* Keep asynchronous operations async through the call chain. Avoid `.Result`, `.Wait()`, and other synchronous waits that can block threads or deadlock.
* Accept and propagate `CancellationToken` through work that can take meaningful time or be cancelled by its caller.
* Bound concurrency for large or unbounded workloads. Avoid creating a task per item or using uncontrolled `Task.WhenAll`; use an appropriate queue or concurrency limiter when work must be throttled.
* Use `ValueTask` only when profiling supports it and its usage contract is clear; otherwise prefer `Task`.
* Keep shared state safe under concurrent access and preserve cancellation and exception behavior when coordinating tasks. Prefer immutable messages or synchronization primitives with clear ownership over unsynchronized shared mutation.
* Avoid holding locks across `await`, blocking thread-pool threads, and starting background work whose lifetime is detached from its owner.
* For producer/consumer work, apply backpressure and bound buffered items so a fast producer cannot exhaust memory.
* Retry only transient failures, with bounded attempts and delay. Preserve idempotency and cancellation, and avoid multiplying retries across nested layers.