Item 13: Initialize Static Class Members with Static Constructors
用靜態(tài)構(gòu)造函數(shù)初始化靜態(tài)類成員
You know that you should initialize static member variables in a type before you create any instances of that type. C# lets you use static initializers and a static constructor for this purpose. A static constructor is a special function that executes before any other methods, variables, or properties defined in that class are accessed. You use this function to initialize static variables, enforce the singleton pattern, or perform any other necessary work before a class is usable. You should not use your instance constructors, some special private function, or any other idiom to initialize static variables.
在創(chuàng)建一個類型的任何實例之前,應(yīng)該初始化其中的靜態(tài)成員變量。C#允許使用靜態(tài)初始化器和靜態(tài)構(gòu)造函數(shù)來達到目的。靜態(tài)構(gòu)造函數(shù)是一個特殊的方法,它在類中任何其它方法、變量或者屬性被訪問之前被執(zhí)行。在一個類被使用前,用該方法初始化靜態(tài)變量,強制單件模式,或者執(zhí)行任何其它必要的工作。不應(yīng)該使用實例構(gòu)造函數(shù)、一些特殊的私有方法、任何其它習(xí)慣來初始化靜態(tài)變量。
As with instance initialization, you can use the initializer syntax as an alternative to the static constructor. If you simply need to allocate a static member, use the initializer syntax. When you have more complicated logic to initialize static member variables, create a static constructor.
和實例初始化一樣,可以使用初始化器語法作為靜態(tài)構(gòu)造函數(shù)的變體。如果你需要簡單的分配一個靜態(tài)成員,那么可以使用初始化器語法。當你有更復(fù)雜的邏輯來初始化靜態(tài)成員變量時,請創(chuàng)建靜態(tài)構(gòu)造函數(shù)。
Implementing the singleton pattern in C# is the most frequent use of a static constructor. Make your instance constructor private, and add an initializer:
考試大提示: 在C#里面實現(xiàn)單件模式,是對靜態(tài)構(gòu)造函數(shù)最頻繁的使用。
public class MySingleton
{
private static readonly MySingleton theOneAndOnly = new MySingleton();
public static MySingleton TheOnly
{
get
{
return theOneAndOnly;
}
}
private MySingleton()
{
}
// remainder elided
}
The singleton pattern can just as easily be written this way,in case you have more complicated logic to initialize the singleton:
考試大提示: 單件模式可以像這樣就這么簡單的寫出來。萬一,你有更復(fù)雜的邏輯要初始化該單件:
public class MySingleton
{
private static readonly MySingleton theOneAndOnly;
static MySingleton()
{
TheOneAndOnly = new MySingleton();
}
public static MySingleton TheOnly
{
get
{
return _theOneAndOnly;
}
}
private MySingleton()
{
}
// remainder elided
}
As with instance initializers, the static initializers are called before any static constructors are called. And, yes, your static initializers execute before the base class's static constructor.
和實例初始化器一樣,靜態(tài)初始化器在任何靜態(tài)構(gòu)造函數(shù)被調(diào)用之前被調(diào)用。同時,靜態(tài)初始化器在基類的靜態(tài)構(gòu)造函數(shù)之前被執(zhí)行。當你的類型首次被加載到應(yīng)用程序空間時,
The CLR calls your static constructor automatically when your type is first loaded into an application space. You can define only one static constructor, and it must not take any arguments. Because static constructors are called by the CLR, you must be careful about exceptions generated in them. If you let an exception escape a static constructor, the CLR will terminate your program. Exceptions are the most common reason to use the static constructor instead of static initializers. If you use static initializers, you cannot catch the exceptions yourself. With a static constructor, you can (see Item 45):
CLR自動調(diào)用靜態(tài)構(gòu)造函數(shù)。你可以僅僅定義一個靜態(tài)構(gòu)造函數(shù),它必須不帶任何參數(shù)。因為靜態(tài)構(gòu)造函數(shù)是由CLR調(diào)用的,你必須當心在它們中出現(xiàn)的異常。如果讓異常跳出靜態(tài)構(gòu)造函數(shù),CLR將會終止你的程序。使用靜態(tài)構(gòu)造函數(shù)而不是靜態(tài)初始化器的最常見的原因就是異常處理。如果使用靜態(tài)初始化器,就不可能自己捕獲異常了。有了靜態(tài)構(gòu)造函數(shù),才可以。(看 Item 45)
static MySingleton()
{
try
{
theOneAndOnly = new MySingleton();
}
catch
{
// Attempt recovery here.
}
}
Static initializers and static constructors provide the cleanest, clearest way to initialize static members of your class. They are easy to read and easy to get correct. They were added to the language to specifically address the difficulties involved with initializing static members in other languages.
靜態(tài)初始化器和靜態(tài)構(gòu)造函數(shù)提供了最潔凈的、最直接的方式來初始化類的靜態(tài)成員。它們?nèi)菀组喿x,容易寫對。它們被加入到語言中來,對在其它語言中涉及到的和初始化靜態(tài)成員相關(guān)的困難,進行了特殊的描述
用靜態(tài)構(gòu)造函數(shù)初始化靜態(tài)類成員
You know that you should initialize static member variables in a type before you create any instances of that type. C# lets you use static initializers and a static constructor for this purpose. A static constructor is a special function that executes before any other methods, variables, or properties defined in that class are accessed. You use this function to initialize static variables, enforce the singleton pattern, or perform any other necessary work before a class is usable. You should not use your instance constructors, some special private function, or any other idiom to initialize static variables.
在創(chuàng)建一個類型的任何實例之前,應(yīng)該初始化其中的靜態(tài)成員變量。C#允許使用靜態(tài)初始化器和靜態(tài)構(gòu)造函數(shù)來達到目的。靜態(tài)構(gòu)造函數(shù)是一個特殊的方法,它在類中任何其它方法、變量或者屬性被訪問之前被執(zhí)行。在一個類被使用前,用該方法初始化靜態(tài)變量,強制單件模式,或者執(zhí)行任何其它必要的工作。不應(yīng)該使用實例構(gòu)造函數(shù)、一些特殊的私有方法、任何其它習(xí)慣來初始化靜態(tài)變量。
As with instance initialization, you can use the initializer syntax as an alternative to the static constructor. If you simply need to allocate a static member, use the initializer syntax. When you have more complicated logic to initialize static member variables, create a static constructor.
和實例初始化一樣,可以使用初始化器語法作為靜態(tài)構(gòu)造函數(shù)的變體。如果你需要簡單的分配一個靜態(tài)成員,那么可以使用初始化器語法。當你有更復(fù)雜的邏輯來初始化靜態(tài)成員變量時,請創(chuàng)建靜態(tài)構(gòu)造函數(shù)。
Implementing the singleton pattern in C# is the most frequent use of a static constructor. Make your instance constructor private, and add an initializer:
考試大提示: 在C#里面實現(xiàn)單件模式,是對靜態(tài)構(gòu)造函數(shù)最頻繁的使用。
public class MySingleton
{
private static readonly MySingleton theOneAndOnly = new MySingleton();
public static MySingleton TheOnly
{
get
{
return theOneAndOnly;
}
}
private MySingleton()
{
}
// remainder elided
}
The singleton pattern can just as easily be written this way,in case you have more complicated logic to initialize the singleton:
考試大提示: 單件模式可以像這樣就這么簡單的寫出來。萬一,你有更復(fù)雜的邏輯要初始化該單件:
public class MySingleton
{
private static readonly MySingleton theOneAndOnly;
static MySingleton()
{
TheOneAndOnly = new MySingleton();
}
public static MySingleton TheOnly
{
get
{
return _theOneAndOnly;
}
}
private MySingleton()
{
}
// remainder elided
}
As with instance initializers, the static initializers are called before any static constructors are called. And, yes, your static initializers execute before the base class's static constructor.
和實例初始化器一樣,靜態(tài)初始化器在任何靜態(tài)構(gòu)造函數(shù)被調(diào)用之前被調(diào)用。同時,靜態(tài)初始化器在基類的靜態(tài)構(gòu)造函數(shù)之前被執(zhí)行。當你的類型首次被加載到應(yīng)用程序空間時,
The CLR calls your static constructor automatically when your type is first loaded into an application space. You can define only one static constructor, and it must not take any arguments. Because static constructors are called by the CLR, you must be careful about exceptions generated in them. If you let an exception escape a static constructor, the CLR will terminate your program. Exceptions are the most common reason to use the static constructor instead of static initializers. If you use static initializers, you cannot catch the exceptions yourself. With a static constructor, you can (see Item 45):
CLR自動調(diào)用靜態(tài)構(gòu)造函數(shù)。你可以僅僅定義一個靜態(tài)構(gòu)造函數(shù),它必須不帶任何參數(shù)。因為靜態(tài)構(gòu)造函數(shù)是由CLR調(diào)用的,你必須當心在它們中出現(xiàn)的異常。如果讓異常跳出靜態(tài)構(gòu)造函數(shù),CLR將會終止你的程序。使用靜態(tài)構(gòu)造函數(shù)而不是靜態(tài)初始化器的最常見的原因就是異常處理。如果使用靜態(tài)初始化器,就不可能自己捕獲異常了。有了靜態(tài)構(gòu)造函數(shù),才可以。(看 Item 45)
static MySingleton()
{
try
{
theOneAndOnly = new MySingleton();
}
catch
{
// Attempt recovery here.
}
}
Static initializers and static constructors provide the cleanest, clearest way to initialize static members of your class. They are easy to read and easy to get correct. They were added to the language to specifically address the difficulties involved with initializing static members in other languages.
靜態(tài)初始化器和靜態(tài)構(gòu)造函數(shù)提供了最潔凈的、最直接的方式來初始化類的靜態(tài)成員。它們?nèi)菀组喿x,容易寫對。它們被加入到語言中來,對在其它語言中涉及到的和初始化靜態(tài)成員相關(guān)的困難,進行了特殊的描述