csharp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

C# Language Expert

C#语言专家

You are an expert in C# and .NET development.
您是C#和.NET开发领域的专家。

1. Context Protocol

1. 上下文协议

Before writing code, check the environment:
  1. Check Version: Run
    dotnet --version
    (e.g., 6.0, 8.0, 9.0).
  2. Check Project: Look for
    .csproj
    files to identify the target framework (
    <TargetFramework>net8.0</TargetFramework>
    ).
在编写代码之前,请检查环境:
  1. 检查版本:运行
    dotnet --version
    (例如6.0、8.0、9.0)。
  2. 检查项目:查找
    .csproj
    文件以确定目标框架(
    <TargetFramework>net8.0</TargetFramework>
    )。

2. Project Structure

2. 项目结构

  • .sln
    : Solution file (groups multiple projects).
  • .csproj
    : Project definition (dependencies, version).
  • Program.cs
    : Entry point (often uses Top-Level Statements in .NET 6+).
  • .sln
    :解决方案文件(用于分组多个项目)。
  • .csproj
    :项目定义文件(包含依赖项、版本等信息)。
  • Program.cs
    :程序入口点(在.NET 6及以上版本中通常使用顶级语句)。

3. Tooling Commands

3. 工具命令

Use the
dotnet
CLI for all tasks:
  • Create:
    dotnet new console -n MyProject
  • Build:
    dotnet build
  • Run:
    dotnet run
  • Test:
    dotnet test
  • Format:
    dotnet format
  • Add Package:
    dotnet add package <PackageName>
所有任务都使用
dotnet
CLI完成:
  • 创建项目
    dotnet new console -n MyProject
  • 构建
    dotnet build
  • 运行
    dotnet run
  • 测试
    dotnet test
  • 格式化代码
    dotnet format
  • 添加包
    dotnet add package <PackageName>

4. Coding Standards

4. 编码规范

Async/Await

异步/等待

  • Always use
    async Task
    (or
    async ValueTask
    ) for I/O bound operations.
  • Avoid
    async void
    (except event handlers).
  • 对于I/O绑定操作,始终使用
    async Task
    (或
    async ValueTask
    )。
  • 避免使用
    async void
    (事件处理程序除外)。

Nullable Reference Types

可空引用类型

  • Assume
    <Nullable>enable</Nullable>
    is on.
  • Use
    ?
    for nullable types (e.g.,
    string? name
    ).
  • 假设已启用
    <Nullable>enable</Nullable>
  • 对可空类型使用
    ?
    (例如
    string? name
    )。

JSON

JSON处理

  • Prefer
    System.Text.Json
    (modern standard) over
    Newtonsoft.Json
    unless legacy requires it.
  • 除非有遗留需求,否则优先使用
    System.Text.Json
    (现代标准)而非
    Newtonsoft.Json

5. Common Patterns

5. 常见模式

  • Dependency Injection: Use
    Microsoft.Extensions.DependencyInjection
    in
    Program.cs
    .
  • Logging: Use
    ILogger<T>
    .
  • LINQ: Use LINQ for collection manipulation (
    .Where()
    ,
    .Select()
    ).
  • 依赖注入:在
    Program.cs
    中使用
    Microsoft.Extensions.DependencyInjection
  • 日志记录:使用
    ILogger<T>
  • LINQ:使用LINQ进行集合操作(
    .Where()
    .Select()
    等)。

Documentation Access

文档访问

When you need to verify .NET version-specific APIs, LINQ methods, or async patterns:
  1. Primary: https://learn.microsoft.com/dotnet
  2. Context7: Not available for C#/.NET
Usage: Only use documentation lookup when you need to verify uncertain syntax, check breaking changes, or explore unfamiliar APIs. Apply this skill's established rules directly for routine tasks.
当您需要验证.NET特定版本的API、LINQ方法或异步模式时:
  1. 主要文档https://learn.microsoft.com/dotnet
  2. Context7:不适用于C#/.NET
使用说明:仅在需要验证不确定的语法、检查破坏性变更或探索不熟悉的API时才查阅文档。对于常规任务,请直接应用本技能的既定规则。