AgentSkillsCN

csharp

遵循现代最佳实践,编写可投入生产环境的 C# 与 .NET 代码。在构建 .NET 应用程序、编写 async/await 代码、使用 Entity Framework Core、实施依赖注入、配置可空引用类型,或优化 C# 性能时,可使用此技能。

SKILL.md
--- frontmatter
name: "csharp"
description: 'Write production-ready C# and .NET code following modern best practices. Use when building .NET applications, writing async/await code, using Entity Framework Core, implementing dependency injection, configuring nullable reference types, or optimizing C# performance.'
metadata:
  author: "AgentX"
  version: "1.0.0"
  created: "2025-01-15"
  updated: "2025-01-15"
compatibility:
  languages: ["csharp"]
  frameworks: ["dotnet", "aspnet-core"]
  platforms: ["windows", "linux", "macos"]

C# / .NET Development

Purpose: Production-ready C# and .NET development standards for building secure, performant, maintainable applications.
Audience: Engineers building .NET applications with C#, ASP.NET Core, Entity Framework Core.
Standard: Follows github/awesome-copilot .NET development patterns.


When to Use This Skill

  • Building .NET applications with C#
  • Writing async/await code patterns
  • Using Entity Framework Core for data access
  • Implementing dependency injection
  • Configuring nullable reference types

Prerequisites

  • .NET 8+ SDK installed
  • C# 12+ language features
  • IDE with C# support

Quick Reference

NeedSolutionPattern
Async codeUse async/await everywhereasync Task<T> GetDataAsync()
Null safetyEnable nullable reference types<Nullable>enable</Nullable>
Error handlingUse Result types or exceptionstry-catch with specific types
DIConstructor injection with interfacesIServiceCollection
TestingxUnit, NUnit, or TUnit[Fact], [Test]
LoggingILogger<T> with structured logging_logger.LogInformation("User {UserId}", id)

C# Language Version

Current: C# 14 (.NET 10+)
Minimum: C# 8 (.NET Core 3.1+)

Modern C# Features (Use These)

csharp
// File-scoped namespaces (C# 10+)
namespace MyApp.Services;

// Primary constructors (C# 12+)
public class UserService(ILogger<UserService> logger, IUserRepository repo)
{
    public async Task<User> GetUserAsync(int id) => 
        await repo.GetByIdAsync(id);
}

// Required properties (C# 11+)
public class User
{
    public required int Id { get; init; }
    public required string Name { get; init; }
}

// Raw string literals (C# 11+)
string json = """
    {
        "name": "John",
        "age": 30
    }
    """;

// Pattern matching
string GetStatus(Order order) => order switch
{
    { Status: "pending", TotalAmount: > 1000 } => "High value pending",
    { Status: "shipped" } => "In transit",
    { Status: "delivered" } => "Completed",
    _ => "Unknown"
};

Common Pitfalls

IssueProblemSolution
Sync over asyncUsing .Result or .Wait()Always use await
No cancellationLong operations can't be cancelledPass CancellationToken
N+1 queriesLoading related data in loopUse .Include() for eager loading
Magic stringsHardcoded strings everywhereUse nameof() or constants
No null checksNullReferenceExceptionEnable nullable reference types
Poor loggingUnstructured log messagesUse structured logging with parameters

Resources


See Also: Skills.mdAGENTS.md

Last Updated: January 27, 2026

Scripts

ScriptPurposeUsage
scaffold-solution.ps1Create .NET solution with API/Core/Infrastructure + xUnit tests./scripts/scaffold-solution.ps1 -Name MyApp [-Framework net9.0]

Troubleshooting

IssueSolution
Deadlock with async codeUse async/await all the way up, never call .Result or .Wait() on async methods
EF Core migration errorsRun dotnet ef migrations list to check state, recreate if corrupted
Nullable warnings everywhereEnable Nullable in .csproj, fix warnings incrementally

References