C# Async Best Practices

C# Async Best Practices

Applies C# async programming best practices to the user's code, covering naming, return types, exceptions, performance and common pitfalls.

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

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…