when i go through the below code, i couldnt find the reason why it using private constructor in the sample?
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;
}
}
}
instead of public class if i created a static class, i can use the class directly rather than creating instance. what is the need of creating a private constructor here, when the static keyword persists to the same job?
any other advantage for following this pattern?
static Singleton()
unless you have to. – Cassiodorus