CSharp Coding Format
命名规范
C#对命名有非常明确的社区共识
- 类型/类/方法 -> PascalCase(大驼峰)
public class PlayerController
{
public void MoveForward() {}
}
- 局部变量/参数 -> camelCase(小驼峰)
int playerHealth;
void SetHealth(int newHealth) {}
- 私有字段 ->
_camelCase
private int _health;
private Transform _target;
- 常量 -> PascalCase或ALL_CAPS
public const int MaxPlayerCount = 4;
- 接口 ->
I前缀
public interface IMovable
{
void Move()
}
- 布尔变量 -> 语义清晰(is/has/can)
bool isDead;
bool hasWeapon;
bool canJump;
代码结构规范
成员顺序
推荐顺序
public class Example
{
// 1. 常量
// 2. 字段(private -> protected -> public)
// 3. 属性
// 4. 构造函数
// 5. 公共方法
// 6. 私有方法
}
using 顺序
using System;
using System.Collections.Generic;
using UnityEngine; // 第三方/框架
系统 -> 第三方 -> 项目内
一个文件一个类
// Player.cs
class Player {}
属性 vs 字段
不推荐直接暴露字段
public int health;
推荐属性
public int Health { get; private set; }
格式规范
花括号不省略
if (isDead)
{
return;
}
// 不要
if (isDead) return;
函数设计规范
SRP
方法名要表达行为
CalculateDamage()
LoadConfig()
InitializeRenderer()
而非
DoStuff()
Handle()
Process()
参数数量小于等于3
注释规范
优先写“为什么”,不是“做什么”
不推荐
// 设置血量
_health = 10;
推荐
// 防止玩家初始血量为0导致死亡动画触发
_health = 10;
XML注释(公共API)
/// <summary>
/// 计算伤害值
/// </summary>
public int CalculateDamage() {}