0

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; } }
}
Community
  • 1
  • 1
Dave
  • 8,163
  • 11
  • 67
  • 103
  • 1
    If no instance constructor is specified than an implicit public default constructor is created so, you need the private default constructor. Otherwise, I could create an instance without using the static `Instance` property. – Jodrell Sep 25 '12 at 11:23
  • This ground has already been covered and the definitive answer is here, for more detail follow the link to @JonSkeet 's blog.http://stackoverflow.com/a/2550935/659190 – Jodrell Sep 25 '12 at 11:28
  • I believe you need the static ctor but for reasons I don't recall, related to http://msmvps.com/blogs/jon_skeet/archive/2010/01/26/type-initialization-changes-in-net-4-0.aspx – Jodrell Sep 25 '12 at 11:29
  • @Jodrell: you need the static ctor to force static member initialization before accessing other static members/properties. Details are explained [here](http://csharpindepth.com/Articles/General/Beforefieldinit.aspx) – huysentruitw Sep 25 '12 at 11:31
  • @WouterH No, it would also work without an explicit static constructor, it's just that without it, the field initializer may be executed at a point where you don't expect it. –  Sep 25 '12 at 11:33

2 Answers2

5

My question is, on .NET 4.0, should I include the constructors (I think they are now implicitly created).

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 following seems to work just as well,

The point is that var s = new Singleton(); should not work. That's the thing to test.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 2
    There is a subtle difference when the static constructor is included. Including it means the singleton is constructed *exactly* when it is first used. Not including it allows it to be constructed a little bit earlier, allowing for some optimizations such as then the singleton is accessed in a loop. –  Sep 25 '12 at 11:26
  • @hvd - Yes, there is a (implementation depended) diff in timing. Your code should never rely or depend on such details. – H H Sep 25 '12 at 11:40
1

Unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation. For more information, see Static Classes and Static Class Members.

Sealed keyword only makes it non-inheritable.

You can prevent a class from being instantiated by making the constructor private. If you don't include the constructors, it won't respect the singleton design pattern. So basically, yes, you should include those as well if you want a true Singleton..

And as hvd's comment pointed out, you should include the other constructor as well.

Source(s): Official MSDND C#

LonWolf
  • 132
  • 4