| 項目 | 規則 | 範例 |
|---|---|---|
| 類別、方法、公開屬性 | PascalCase | public class TextBox { public void DataBind() {} } |
| 區域變數、方法參數 | camelCase | string userName; void AddUser(string userId, byte[] passWord) |
| 私有成員欄位 | 前綴 _ | private string _connectionString; |
| 介面 | 前綴 I | interface ICompare { int Compare(); } |
| 自訂屬性類別 | 以 Attribute 結尾 | public class AuthorAttribute : Attribute {} |
| 自訂例外類別 | 以 Exception 結尾 | public class AppException : Exception {} |
| 方法命名 | 動賓片語 | ShowDialog(), CreateFile(), GetPath() |
{ 另起一行 (Allman 風格)。string, int) 而非完整型別名 (System.String, System.Int32)。const 或 enum。goto。add/remove) 封裝。as 搭配 null 檢查:Dog dog = animal as Dog; if (dog != null) { ... }StringBuilder。if/else/for/while 區塊永遠加大括號,即使單行。switch 必須有 default 處理非預期情況。?:,改用 if-else 增加可讀性。this.,僅在建構函式鏈結時使用:public Person() : this("Jim") {}using 指令:系統命名空間與自訂/第三方命名空間間空一行分隔。一致的編碼風格能大幅提升團隊協作效率、降低維護成本。建議專案導入靜態分析工具 (如 StyleCop、Roslyn Analyzers) 自動強制檢查。