以下のコードを実行すると、サンプルでプライベート コンストラクターを使用する理由が見つかりませんでした。
public sealed class Singleton
{
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
...
//Why shouldnt I use something like below.
public class Singleton
{
private static Singleton instance = null;
static Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
静的クラスを作成した場合、パブリック クラスの代わりに、インスタンスを作成するのではなく、クラスを直接使用できます。static キーワードが同じジョブに持続する場合、ここでプライベートコンストラクターを作成する必要は何ですか?
このパターンに従うことの他の利点はありますか?