方法 是執行計算或行為的成員。宣告語法:
method-header method-body
attributes method-modifiers return-type member-name(formal-parameter-list)
new、public、protected、internal、privatestatic、virtual、sealed、override、abstract、externabstract/extern 方法主體僅 ;;其餘需包含執行敘述。
return 敘述回傳值;無值 return; 對應 void。
public int Max(int x, int y) {
if (x > y) return x;
else return y;
}
Console.WriteLine($"Max of 6 and 8: {Max(6,8)}"); // 8
方法名不可與同類別其他方法/成員同名。