文件
-
靜態類別無法在 C# 中具現化。 您可以使用類別名稱本身來存取靜態類別的成員。
-
C# 的介面可以宣告屬性。 本範例會定義介面屬性存取子。
-
C# 中所有的類型和類型成員,都有其存取範圍層級,可以控制能否從其他程式碼使用類型及其成員。 檢閱此存取修飾詞清單。
靜態建構函式用來初始化任何靜態資料,或執行只需要執行一次的特定動作。 在建立第一個實例之前或引用任何靜態成員時,會自動呼叫它。 靜態建構函式最多會呼叫一次。
class SimpleClass
{
// Static variable that must be initialized at run time.
static readonly long baseline;
// Static constructor is called at most one time, before any
// instance constructor is invoked or member is accessed.
static SimpleClass()
{
baseline = DateTime.Now.Ticks;
}
}
有數個動作屬於靜態初始化的一部分。 這些動作會依下列順序進行:
重要
在建立任何執行個體之前,靜態建構函式執行的規則有一項重要的例外。 如果靜態欄位初始設定式建立該類型的執行個體,該初始設定式會在靜態建構函式執行之前執行 (包括對執行個體建構函式的任何呼叫)。 這在單一模式中最常見,如下列範例所示:
public class Singleton
{
// Static field initializer calls instance constructor.
private static Singleton instance = new Singleton();
private Singleton()
{
Console.WriteLine("Executes before static constructor.");
}
static Singleton()
{
Console.WriteLine("Executes after instance constructor.");
}
public static Singleton Instance => instance;
}
模組初始設定式可以是靜態建構函式的替代方案。 如需詳細資訊,請參閱模組初始設定式的規格 (英文)。
靜態建構函式具有下列屬性:
static readonly
的欄位,只能指派為其宣告的一部分,或在靜態建構函式中指派。 當不需要明確靜態建構函式時,請在宣告時初始化靜態欄位,而不要透過靜態建構函式,以獲得更好的執行階段最佳化。Parallel.Invoke
和平行 LINQ 查詢。注意
雖然無法直接存取明確靜態建構函式,但應記錄其存在,以協助對初始化例外狀況進行疑難排解。
LoadLibrary
方法時。在此範例中,Bus
類別具有靜態建構函式。 建立 Bus
的第一個執行個體 (bus1
) 時,即會叫用靜態建構函式來初始化類別。 範例輸出可確認即使建立了兩個 Bus
執行個體,靜態建構函式也只執行一次,並且它會在執行個體建構函式之前執行。
public class Bus
{
// Static variable used by all Bus instances.
// Represents the time the first bus of the day starts its route.
protected static readonly DateTime globalStartTime;
// Property for the number of each bus.
protected int RouteNumber { get; set; }
// Static constructor to initialize the static variable.
// It is invoked before the first instance constructor is run.
static Bus()
{
globalStartTime = DateTime.Now;
// The following statement produces the first line of output,
// and the line occurs only once.
Console.WriteLine($"Static constructor sets global start time to {globalStartTime.ToLongTimeString()}");
}
// Instance constructor.
public Bus(int routeNum)
{
RouteNumber = routeNum;
Console.WriteLine($"Bus #{RouteNumber} is created.");
}
// Instance method.
public void Drive()
{
TimeSpan elapsedTime = DateTime.Now - globalStartTime;
// For demonstration purposes we treat milliseconds as minutes to simulate
// actual bus times. Do not do this in your actual bus schedule program!
Console.WriteLine($"{this.RouteNumber} is starting its route {elapsedTime.Milliseconds:N2} minutes after global start time {globalStartTime.ToShortTimeString()}.");
}
}
class TestBus
{
static void Main()
{
// The creation of this instance activates the static constructor.
Bus bus1 = new Bus(71);
// Create a second bus.
Bus bus2 = new Bus(72);
// Send bus1 on its way.
bus1.Drive();
// Wait for bus2 to warm up.
System.Threading.Thread.Sleep(25);
// Send bus2 on its way.
bus2.Drive();
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/* Sample output:
Static constructor sets global start time to 3:57:08 PM.
Bus #71 is created.
Bus #72 is created.
71 is starting its route 6.00 minutes after global start time 3:57 PM.
72 is starting its route 31.00 minutes after global start time 3:57 PM.
*/
文件
靜態類別無法在 C# 中具現化。 您可以使用類別名稱本身來存取靜態類別的成員。
C# 的介面可以宣告屬性。 本範例會定義介面屬性存取子。
C# 中所有的類型和類型成員,都有其存取範圍層級,可以控制能否從其他程式碼使用類型及其成員。 檢閱此存取修飾詞清單。
訓練