トレーニング
モジュール
コードの読みやすさ、保守容易性、および編成を改善できる静的クラス、部分クラス、オブジェクト初期化子などの高度な手法を使用してクラスを実装する方法について説明します。
このブラウザーはサポートされなくなりました。
Microsoft Edge にアップグレードすると、最新の機能、セキュリティ更新プログラム、およびテクニカル サポートを利用できます。
静的コンストラクターは、任意の静的データを初期化するため、または 1 回だけ実行する必要がある特定のアクションを実行するために使用されます。 最初のインスタンスが作成される前、または静的メンバーが参照される前に、自動的に呼び出されます。 静的コンストラクターが呼び出されるのは多くても 1 回です。
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;
}
}
静的な初期化の一部であるアクションがいくつかあります。 これらのアクションは、次の順序で実行されます。
重要
インスタンスの作成前に静的コンストラクターが実行されるというルールには、1 つの重要な例外があります。 静的フィールド初期化子が型のインスタンスを作成する場合、その初期化子は静的コンストラクターの実行前に実行されます (インスタンス コンストラクターの呼び出しを含みます)。 次の例で示すように、これは "シングルトン パターン" で最も一般的です。
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
、Parallel LINQ クエリなど) のブロックを実行したりしないでください。注意
直接アクセスすることはできませんが、初期化の例外のトラブルシューティングを助けるため、明示的な静的コンストラクターが存在することを文書化する必要があります。
LoadLibrary
メソッドを呼び出すことができる場合は、アンマネージ コードのラッパー クラスを作成するときにも静的コンストラクターが役に立ちます。この例では、Bus
クラスに静的コンストラクターがあります。 Bus
の最初のインスタンスが作成されるとき (bus1
)、静的コンストラクターが呼び出されてクラスが初期化されます。 サンプルの出力では、Bus
のインスタンスが 2 つでも静的コンストラクターは 1 回だけ実行されること、およびインスタンス コンストラクターの実行前に静的コンストラクターが実行されることがわかります。
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# 言語仕様の「Static constructors (静的コンストラクター)」セクションをご覧ください。
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。
トレーニング
モジュール
コードの読みやすさ、保守容易性、および編成を改善できる静的クラス、部分クラス、オブジェクト初期化子などの高度な手法を使用してクラスを実装する方法について説明します。