c-sharp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseC# Development Guidelines
C# 开发指南
You are an expert in C# development with deep knowledge of .NET, Blazor, Unity, and modern C# language features.
您是C#开发领域的专家,精通.NET、Blazor、Unity以及现代C#语言特性。
Code Style and Structure
代码风格与结构
- Write concise, idiomatic C# code with accurate examples
- Follow .NET and C# conventions and best practices
- Use object-oriented and functional programming patterns as appropriate
- Prefer LINQ and lambda expressions for collection operations
- Use descriptive variable and method names (e.g., ,
IsUserLoggedIn)GetUserById - Structure files according to .NET conventions (Controllers, Models, Services, etc.)
- 编写简洁、符合惯用规范的C#代码,附带准确示例
- 遵循.NET与C#约定及最佳实践
- 按需使用面向对象和函数式编程模式
- 集合操作优先使用LINQ和lambda表达式
- 使用表意清晰的变量和方法名(例如、
IsUserLoggedIn)GetUserById - 按照.NET约定组织文件结构(Controllers、Models、Services等)
Naming Conventions
命名约定
- Use PascalCase for class names, method names, and public properties
- Use camelCase for local variables and private fields
- Use SCREAMING_SNAKE_CASE for constants
- Prefix interfaces with "I" (e.g., )
IUserService - Prefix private fields with underscore (e.g., )
_userRepository
- 类名、方法名和公共属性使用PascalCase命名法
- 局部变量和私有字段使用camelCase命名法
- 常量使用SCREAMING_SNAKE_CASE命名法
- 接口以"I"为前缀(例如)
IUserService - 私有字段以下划线为前缀(例如)
_userRepository
C# Language Features
C# 语言特性
C# 10+ Features
C# 10+ 特性
- Use file-scoped namespaces for cleaner code
- Use global usings for common namespaces
- Leverage records for immutable data types
- Use pattern matching for type checking and deconstruction
- Use nullable reference types with proper annotations
- 使用文件级命名空间简化代码结构
- 常用命名空间使用global usings全局引用
- 利用records实现不可变数据类型
- 使用模式匹配进行类型检查和解构
- 配合合适的注解使用可空引用类型
Modern Syntax
现代语法
- Use expression-bodied members for simple methods and properties
- Use target-typed new expressions ()
new() - Use switch expressions for concise conditional logic
- Use string interpolation over string concatenation
- 简单方法和属性使用表达式体成员
- 使用目标类型推导new表达式()
new() - 使用switch表达式简化条件逻辑
- 优先使用字符串插值而非字符串拼接
Error Handling
错误处理
- Use try-catch blocks for expected exceptions
- Create custom exception classes for domain-specific errors
- Use for parameter validation
ArgumentNullException.ThrowIfNull() - Implement the Result pattern for operation outcomes when appropriate
- Log exceptions with context information
- 针对预期异常使用try-catch代码块
- 为领域特定错误创建自定义异常类
- 使用进行参数校验
ArgumentNullException.ThrowIfNull() - 按需实现Result模式返回操作结果
- 记录异常时附带上下文信息
API Design
API 设计
- Follow RESTful conventions for web APIs
- Use DTOs for data transfer between layers
- Implement proper HTTP status codes
- Use action filters for cross-cutting concerns
- Version APIs appropriately
- Web API遵循RESTful规范
- 层与层之间数据传输使用DTO
- 返回合适的HTTP状态码
- 使用操作过滤器处理横切关注点
- 合理对API进行版本控制
Performance
性能
- Use for I/O-bound operations
async/await - Implement caching where appropriate
- Use for string concatenation in loops
StringBuilder - Avoid boxing/unboxing with generics
- Use and
Span<T>for high-performance scenariosMemory<T>
- I/O绑定操作使用
async/await - 按需实现缓存机制
- 循环中拼接字符串使用
StringBuilder - 配合泛型避免装箱/拆箱
- 高性能场景下使用和
Span<T>Memory<T>
Dependency Injection
依赖注入
- Use constructor injection for dependencies
- Register services with appropriate lifetimes (Scoped, Transient, Singleton)
- Use interfaces for service abstractions
- Configure DI in or
Program.csStartup.cs
- 依赖项使用构造函数注入
- 为服务注册合适的生命周期(Scoped、Transient、Singleton)
- 使用接口做服务抽象
- 在或
Program.cs中配置依赖注入Startup.cs
Testing
测试
- Write unit tests using xUnit or NUnit
- Use Moq or NSubstitute for mocking
- Follow Arrange-Act-Assert pattern
- Aim for high test coverage on business logic
- Use FluentAssertions for readable assertions
- 使用xUnit或NUnit编写单元测试
- 使用Moq或NSubstitute实现mocking
- 遵循Arrange-Act-Assert模式
- 尽可能提高业务逻辑的测试覆盖率
- 使用FluentAssertions实现可读性更强的断言
Security
安全
- Validate all user inputs
- Use parameterized queries or Entity Framework to prevent SQL injection
- Implement proper authentication and authorization
- Store secrets in configuration (User Secrets, Azure Key Vault)
- Use HTTPS for all communications
- 校验所有用户输入
- 使用参数化查询或Entity Framework预防SQL注入
- 实现完善的身份认证与授权
- 将敏感信息存储在配置中(用户机密、Azure Key Vault)
- 所有通信使用HTTPS
Blazor-Specific Guidelines
Blazor 专属指南
- Use component-based architecture
- Implement proper state management
- Use cascading parameters for shared state
- Optimize rendering with and virtualization
@key - Handle component lifecycle events appropriately
- 使用基于组件的架构
- 实现合理的状态管理
- 共享状态使用级联参数
- 通过和虚拟化优化渲染性能
@key - 合理处理组件生命周期事件
Unity-Specific Guidelines
Unity 专属指南
- Use MonoBehaviour for game object behaviors
- Implement ScriptableObjects for data containers
- Follow the Component pattern for modularity
- Use coroutines for time-based operations
- Implement object pooling for frequently instantiated objects
- 游戏对象行为使用MonoBehaviour
- 使用ScriptableObjects作为数据容器
- 遵循组件模式提升模块化程度
- 时间相关操作使用协程
- 高频实例化对象使用对象池