静的コンストラクタの必要性を理解しようとしています。私が見つけた情報のどれも、私が持っている質問に答えたものはありませんこの静的コンストラクタを理解しようとしています
class SimpleClass
{
// Static variable that must be initialized at run time.
static readonly long baseline = DateTime.Now.Ticks;
// Static constructor is called at most one time, before any
// instance constructor is invoked or member is accessed.
//static SimpleClass()
//{
//}
}
とは対照的に、なぜあなたはこの
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;
}
}
をしますか?
これは他の質問とは異なりますが、これはパラメータを受け付けない静的コンストラクタに関するものです。
これは静的な特定の質問ではなく、静的でない初期化に対しても有効です。 –
[クラスのフィールドをコンストラクタで初期化するか、宣言で初期化できますか?](http://stackoverflow.com/questions/24551/initialize-class-field-in-constructor-or-at-declaration) –
@CyrilGandonそうではありませんこれは、パラメータを受け付けない静的コンストラクタに関するものです。 dupeタグを削除することを検討してください。 –