Possible Duplicate:
Singleton by Jon Skeet clarification
I am reading up on Singletons and and now (also thanks to SO) quite clued up.
My implementation (which should be text book) looks like
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
private Singleton(){ }
static Singleton(){ }
public static Singleton Instance { get { return instance; } }
}
My question is, on .NET 4.0, should I include the constructors (I think the private ctor is implicitly created – but what about the static (doubtful)).
The following seems to work just as well, but I’m concerned it only works well in my contrived test example.
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
public static Singleton Instance { get { return instance; } }
}
Yes, there is no change in Fx4 or C# 4.
If you don’t provide the instance constructor then the compiler provides a public one.
There is no reason to provide the static constructor.
The point is that
var s = new Singleton();should not work. That’s the thing to test.