MyClass.cs)MyProject.TestSuite.TestTier → MyProject/TestSuite/TestTier/命名空間層級對應目錄層級,不用點號扁平化,利於導航與建置腳本。
// 推薦:高層級換行
var = a * b / (c - g + f) +
4 * z;
// 避免:括號內換行
var = a * b / (c - g +
f) + 4 * z;
> var = a * b / (c - g + f) + > ......4 * z;(>=Tab, .=Space)////* Line 1 * Line 2 * Line 3 */// 用於註解掉代碼(SharpDevelop Alt+/)
// 單行
/// This class...
// 多行
///
/// This exception gets thrown as soon as a
/// Bogus flag gets set.
///
// 標籤分類:
// 1. 文件項目:summary, param, exception, returns, value...
// 2. 版面/引用:code, list, para, see, seealso...
int level; // indentation level
int size; // size of table
// 避免
int a, b; // a 是什麼?b 代表什麼?
string name = myObject.Name;
int val = time.Hours;
// Dialog 使用 using
using (var dlg = new OpenFileDialog()) { ... }
void Method()
class MySample : MyClass, IMyInterface {
int myInt;
public MySample(int myInt) {
this.myInt = myInt;
}
void Inc() { ++myInt; }
void EmptyMethod() { }
}
return n * (n + 1) / 2;
if (condition) { DoSomething(); }
if (condition) { DoSomething(); } else { DoOther(); }
if (cond1) { ... } else if (cond2) { ... } else { ... }
for (int i = 0; i < 5; ++i) { ... }
foreach (int i in IntList) { ... }
while (condition) { ... }
do { ... } while (condition);
while
switch (condition) {
case A: ... break;
case B: ... break;
default: ... break;
}
try { ... } catch (Exception) { }
try { ... } catch (Exception e) { ... }
try { ... } catch (Exception e) { ... } finally { ... }
Method(a, b, c);a = b; for (int i = 0; i < 10; ++i)
string name = "Mr. Ed";
int myValue = 5;
Test aTest = Test.TestYou;
| 風格 | 規則 | 範例 |
|---|---|---|
| PascalCase | 每個單字首字大寫 | TestCounter |
| camelCase | 首單字小寫,其餘大寫 | testCounter |
| UPPER_CASE | 僅 1-2 字母縮寫 | PI, E |
| 目標 | 規則 | 範例 |
|---|---|---|
| 類別/結構 | 名詞/名詞片語,PascalCase,無前綴 | UserService |
| 介面 | 名詞/形容詞,PascalCase,I 前綴 | IEnumerable |
| 列舉型別/值 | PascalCase,無前後綴,單數名稱 | Color.Red |
| 位元欄位列舉 | 複數名稱 | FileAccessFlags |
| const/readonly 靜態欄位 | PascalCase | MaxConnections |
| 參數/非 const 欄位 | camelCase,語意導向 | userName |
| 區域變數 | camelCase,計數器可用 i,j,k | index |
| 方法 | 動詞/動詞片語,PascalCase | CalculateTotal |
| 屬性 | 名詞/名詞片語,PascalCase | UserName |
| 事件處理器 | EventHandler 後綴,sender/e 參數 | Button_Click |
| 事件參數類別 | EventArgs 後綴 | ClickEventArgs |
| 事件名稱 | 動詞,現在/過去式區分前後 | Opening, Opened |
| 類型 | 大小寫 | 備註 |
|---|---|---|
| Class/Struct | PascalCase | |
| Interface | PascalCase | I 開頭 |
| Enum 值/型別 | PascalCase | |
| Events | PascalCase | |
| Exception 類別 | PascalCase | Exception 結尾 |
| Public 欄位 | PascalCase | |
| Methods | PascalCase | |
| Namespace | PascalCase | |
| Property | PascalCase | |
| Protected/Private 欄位 | camelCase | |
| Parameters | camelCase |
// 避免
if (count > 427) ...
// 採用
public const int MaxUsers = 427;
if (count > MaxUsers) ...
namespace ShowMeTheBracket {
public enum Test { TestMe, TestYou }
public class TestMeClass {
Test test;
public Test Test {
get { return test; }
set { test = value; }
}
void DoSomething() {
if (test == Test.TestMe) { ... }
else { ... }
}
}
}
左大括號換行位置:命名空間、類別/介面/結構、方法宣告
// 避免:i, j 無語意
for (int i = 1; i < num; ++i) { meetsCriteria[i] = true; }
for (int i = 2; i < num / 2; ++i) {
int j = i + i;
while (j <= num) { meetsCriteria[j] = false; j += i; }
}
// 推薦:語意化命名
for (int primeCandidate = 1; primeCandidate < num; ++primeCandidate)
isPrime[primeCandidate] = true;
for (int factor = 2; factor < num / 2; ++factor) {
int factorableNumber = factor + factor;
while (factorableNumber <= num) {
isPrime[factorableNumber] = false;
factorableNumber += factor;
}
}
這份風格指南強調一致性、可讀性、可維護性。核心爭議點在於 Tab 縮排與 3 空白續行對齊,但內部統一即可。建議團隊採用 .editorconfig 落地執行,並整合至 CI 檢查流程。命名慣例與檔案組織是長期受益最高的規範,值得嚴格執行。