| 目標 | 慣例 | 範例 |
|---|---|---|
| 類別、結構、介面、委派、枚舉、事件、屬性、方法、命名空間 | PascalCase | CustomerOrder, IEnumerable, OnClick |
| 介面 | 前綴 I + PascalCase | IDisposable, IUserRepository |
| 泛型類型參數 | 單一大寫字母或 T + PascalCase | T, TKey, TValue |
| 私有/內部欄位 | _camelCase (底線前綴) | _connectionString, _items |
| 區域變數、參數 | camelCase | userName, maxCount |
| 常數 | PascalCase (公開) / _camelCase (私有) | MaxRetryCount, _defaultTimeout |
| 非同步方法 | 後綴 Async | GetUserAsync |
| 布林屬性/方法 | 前綴 Is/Can/Has/Should | IsValid, CanExecute |
| 事件處理器 | On + 事件名 | OnClick, OnPropertyChanged |
| 自訂屬性類別 | 後綴 Attribute | ObsoleteAttribute |
| 自訂例外類別 | 後綴 Exception | ValidationException |
// 推薦
public string Name { get; set; }
public int Age { get; private set; }
public string Id { get; init; } // C# 9+ init-only
public record Person(string Name, int Age); // C# 9+ record (不可變、值語意)
public readonly struct Point { public double X { get; } public double Y { get; } }
// 右側型別明顯時使用
var users = new List();
var count = users.Count;
var config = GetConfig();
// 避免:var x = DoSomething(); // 型別不明
var p = new Person { Name = "Tom", Age = 25 };
var dict = new Dictionary { [1] = "one", [2] = "two" };
var (name, age) = person; // 解構
if (person is { Age: >= 18 }) { ... } // 屬性模式
UserService.cs)MyApp/Services/UserService.cs → MyApp.Services).Generated.cs, .Extensions.cs 等後綴.editorconfig 統一縮排、命名、大括號風格一致的編碼風格能降低認知負擔、減少合併衝突、提升程式碼審查效率。建議團隊依據上述指南制定 .editorconfig 並納入建置流程,新成員上手即符合標準,避免「風格爭論」浪費時間。