I was reading about Singleton design pattern when i come across this implementation:
public class Singleton
{
private static Singleton Instance { get; private set; }
private Singleton()
{
}
static Singleton()
{
Instance = new Singleton();
}
}
Is this singleton thread safe? What are pros and cons of such implementation?
static Singleton Instance { get; } = new Singleton();, a variation that is discussed in the article. Of course, the code you posted is useless, because theInstanceproperty isprivate, and so can only be called by theSingletonclass itself (which has no constraints on how many instances ofSingletonit can create...singletons are really only useful constructs for outside code).