AGENT SKILL

csharp

C# programming language best practices and patterns

适用职业开发工程师
License 许可
MIT
最近更新
2026-03-01
Stars
★ 3
Forks
2

01 前言 Front Matter

name
csharp
license
MIT
metadata
{ "audience": "developers", "category": "programming" }
description
C# programming language best practices and patterns
compatibility
opencode

03 SKILL.md 正文

What I do

  • Write idiomatic C# code
  • Use .NET features effectively
  • Implement async/await patterns
  • Use LINQ for queries
  • Apply SOLID principles
  • Handle exceptions properly
  • Use dependency injection
  • Write unit tests

When to use me

When writing C# or .NET code.

C# Best Practices

// Async/await patterns
public async Task<UserDto> GetUserAsync(int id)
{
    return await _repository.GetByIdAsync(id)
        .ConfigureAwait(false);
}

// LINQ queries
var activeUsers = await _context.Users
    .Where(u => u.IsActive)
    .OrderBy(u => u.CreatedAt)
    .Skip(page * pageSize)
    .Take(pageSize)
    .ToListAsync();

// Dependency injection
services.AddScoped<IUserService, UserService>();
services.AddSingleton<IConfiguration>(Configuration);

// Records for DTOs
public record UserDto(
    int Id,
    string Email,
    string Name,
    DateTime CreatedAt);

// Pattern matching
public decimal CalculateDiscount(Order order) => order switch
{
    { IsPremium: true } => 0.2m,
    { Items.Count: > 10 } => 0.1m,
    { Total: > 100 } => 0.05m,
    _ => 0m
};

04 同仓库更多 Skills