跳到正文
Skillpedia
通用编程40k · #7151

csharp-async

它能做什么

这个 AI 智能体遵循异步编程的最佳实践,帮助你更高效、更安全地编写 C# 代码。它会检查你的代码,确保运行快速,并避免常见错误。

适用场景

  • ✓编写需要同时处理多项任务而又不会卡死的 C# 代码时。
  • ✓希望程序响应更快时。
  • ✓避免在处理耗时操作的 C# 代码中犯常见错误时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/csharp-async/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/csharp-async/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请检查这段 C# 代码,看看我的异步编程用得是否正确:[在此粘贴你的 C# 代码]”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会把你的代码与一套 C# 异步编程的规则和建议进行对照,找出可以改进的地方,包括命名、错误处理、性能,并避开常见陷阱。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

C# Async Programming Best Practices

Your goal is to help me follow best practices for asynchronous programming in C#.

Naming Conventions

  • Use the 'Async' suffix for all async methods
  • Match method names with their synchronous counterparts when applicable (e.g., GetDataAsync() for GetData())

Return Types

  • Return Task<T> when the method returns a value
  • Return Task when the method doesn't return a value
  • Consider ValueTask<T> for high-performance scenarios to reduce allocations
  • Avoid returning void for async methods except for event handlers

Exception Handling

  • Use try/catch blocks around await expressions
  • Avoid swallowing exceptions in async methods
  • Use ConfigureAwait(false) when appropriate to prevent deadlocks in library code
  • Propagate exceptions with Task.FromException() instead of throwing in async Task returning methods

Performance

  • Use Task.WhenAll() for parallel execution of multiple tasks
  • Use Task.WhenAny() for implementing timeouts or taking the first completed task
  • Avoid unnecessary async/await when simply passing through task results
  • Consider cancellation tokens for long-running operations

Common Pitfalls

  • Never use .Wait(), .Result, or .GetAwaiter().GetResult() in async code
  • Avoid mixing blocking and async code
  • Don't create async void methods (except for event handlers)
  • Always await Task-returning methods

Implementation Patterns

  • Implement the async command pattern for long-running operations
  • Use async streams (IAsyncEnumerable<T>) for processing sequences asynchronously
  • Consider the task-based asynchronous pattern (TAP) for public APIs

When reviewing my C# code, identify these issues and suggest improvements that follow these best practices.

常见问题

什么是异步编程?

这是一种编写代码的方式:当程序在等待某个耗时任务(比如从网络获取数据)完成时,可以去做其他事情,而不是干等。

为什么要遵循最佳实践?

遵循最佳实践可以让代码运行更快、更易读、更少出 bug,也更便于你和其他程序员维护。

什么是“死锁”?

死锁是指代码中的两个或多个部分相互等待,永远无法继续,导致程序卡住。

详情

作者的原始描述

Get best practices for C# async programming

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7151
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能