csharp-async
它能做什么
这个 AI 智能体遵循异步编程的最佳实践,帮助你更高效、更安全地编写 C# 代码。它会检查你的代码,确保运行快速,并避免常见错误。
适用场景
- ✓编写需要同时处理多项任务而又不会卡死的 C# 代码时。
- ✓希望程序响应更快时。
- ✓避免在处理耗时操作的 C# 代码中犯常见错误时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/csharp-async/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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()forGetData())
Return Types
- Return
Task<T>when the method returns a value - Return
Taskwhen the method doesn't return a value - Consider
ValueTask<T>for high-performance scenarios to reduce allocations - Avoid returning
voidfor 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,也更便于你和其他程序员维护。
什么是“死锁”?
死锁是指代码中的两个或多个部分相互等待,永远无法继续,导致程序卡住。
详情
热度
- GitHub 星标
- 40k
- 排名
- #7151
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。