csharp-async-patterns

Specify C# async method signatures, Task patterns, and cancellation handling.

4|Updated Dec 7, 2025
One-click install
npx skills add https://github.com/icartsh/icartsh_plugin --skill csharp-async-patterns-icartsh
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-async-patterns
Source: https://github.com/icartsh/icartsh_plugin/tree/main/icartsh-plugin/skills/csharp-async-patterns
Command: npx skills add https://github.com/icartsh/icartsh_plugin --skill csharp-async-patterns-icartsh

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill covers modern C# async/await patterns, Task vs ValueTask, async streams, and cancellation practices for robust asynchronous code.

Core Features & Use Cases

  • Async basics: proper method signatures and await usage
  • Task patterns: composition, parallelism, and error handling
  • ValueTask optimization: when to use to reduce allocations
  • Async streams: practical streaming data patterns

Quick Start

Explore the examples in the documentation and implement sample async methods like FetchDataAsync and ProcessOrderAsync.

Frequently Asked Questions about csharp-async-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What's the difference between Task and ValueTask in C# async code?

Task allocates heap memory for every async operation, while ValueTask is a struct that avoids allocation when the operation completes synchronously. Use ValueTask for hot paths and high-frequency operations to reduce garbage collection pressure; use Task for general-purpose asynchronous work.

How do I write scalable asynchronous code in C# using async/await?

Apply async/await patterns with proper method signatures (async Task or async ValueTask), use await to compose operations, handle cancellation tokens, and orchestrate parallel tasks with Task.WhenAll or Task.WhenAny. Avoid blocking calls like .Result and fire-and-forget patterns outside event handlers.

When should I use async streams instead of regular async methods?

Async streams (IAsyncEnumerable) enable iteration over data that arrives asynchronously over time. Use them for processing paginated API responses, real-time event feeds, or large datasets that can't fit in memory at once, yielding results as they become available.

How do I handle cancellation in C# async operations?

Pass CancellationToken parameters through your async call chain and check token.ThrowIfCancellationRequested() or token.IsCancellationRequested within long-running operations. Wire tokens from UI timeouts, user actions, or scope disposal to propagate cancellation cleanly across tasks.

Can I use async/await patterns across UI, services, and libraries safely?

Yes. Maintain consistent async method signatures, avoid blocking with .Result or .Wait(), use ConfigureAwait(false) in libraries to prevent UI thread deadlocks, and pass cancellation tokens down call chains. Keep fire-and-forget patterns confined to event handlers with proper exception logging.

What async patterns work best for modern C# versions 8–12?

Modern C# supports nullable async methods, async Main, records with async operations, and refined ValueTask handling. Apply these versions' improvements to simplify null-safety in async flows, enable top-level async entry points, and optimize allocations with ValueTask and structured concurrency patterns.