$npx -y skills add github/awesome-copilot --skill csharp-asyncGet best practices for C# async programming
| 1 | # C# Async Programming Best Practices |
| 2 | |
| 3 | Your goal is to help me follow best practices for asynchronous programming in C#. |
| 4 | |
| 5 | ## Naming Conventions |
| 6 | |
| 7 | - Use the 'Async' suffix for all async methods |
| 8 | - Match method names with their synchronous counterparts when applicable (e.g., `GetDataAsync()` for `GetData()`) |
| 9 | |
| 10 | ## Return Types |
| 11 | |
| 12 | - Return `Task<T>` when the method returns a value |
| 13 | - Return `Task` when the method doesn't return a value |
| 14 | - Consider `ValueTask<T>` for high-performance scenarios to reduce allocations |
| 15 | - Avoid returning `void` for async methods except for event handlers |
| 16 | |
| 17 | ## Exception Handling |
| 18 | |
| 19 | - Use try/catch blocks around await expressions |
| 20 | - Avoid swallowing exceptions in async methods |
| 21 | - Use `ConfigureAwait(false)` when appropriate to prevent deadlocks in library code |
| 22 | - Propagate exceptions with `Task.FromException()` instead of throwing in async Task returning methods |
| 23 | |
| 24 | ## Performance |
| 25 | |
| 26 | - Use `Task.WhenAll()` for parallel execution of multiple tasks |
| 27 | - Use `Task.WhenAny()` for implementing timeouts or taking the first completed task |
| 28 | - Avoid unnecessary async/await when simply passing through task results |
| 29 | - Consider cancellation tokens for long-running operations |
| 30 | |
| 31 | ## Common Pitfalls |
| 32 | |
| 33 | - Never use `.Wait()`, `.Result`, or `.GetAwaiter().GetResult()` in async code |
| 34 | - Avoid mixing blocking and async code |
| 35 | - Don't create async void methods (except for event handlers) |
| 36 | - Always await Task-returning methods |
| 37 | |
| 38 | ## Implementation Patterns |
| 39 | |
| 40 | - Implement the async command pattern for long-running operations |
| 41 | - Use async streams (IAsyncEnumerable<T>) for processing sequences asynchronously |
| 42 | - Consider the task-based asynchronous pattern (TAP) for public APIs |
| 43 | |
| 44 | When reviewing my C# code, identify these issues and suggest improvements that follow these best practices. |