public class Test {
static Test() { Console.WriteLine("www.gosoa.com.cn"); }
public Test() {}
static void Main() { Test t = new Test(); }
}
// 輸出:www.gosoa.com.cn(建立實例前靜態建構子已執行)
靜態建構子:無參數、無存取修飾符、類別載入時由 CLR 自動執行一次、最多一個。適用於靜態欄位/屬性從外部資料源初始化。
public class Test {
private string domain, url;
public Test(string dom, string url) { this.domain = dom; this.url = url; }
public Test(string dom) { this.domain = dom; this.url = "gosoa.com.cn"; }
static void Main() { Test t = new Test("gosoa"); Console.WriteLine(t.url); }
}
// 輸出:gosoa.com.cn