Auto-property initializer Singleton implementation

2020-08-22 03:21发布

问题:

So, with the brand new C# 6 we got those neat auto-property initializers. I thought I might as well take advantage of these to make more concise singletons than ever. Apparently someone else got that idea, too.

public sealed class Singleton
{
    public static Singleton Instance { get; } = new Singleton();
    private Singleton() { /* some initialization code */ }
}

My questions are:

  1. How thread-safe it is?
  2. How lazy it is, or when the instance is actually created? (not a priority, but it would be good for future reference)
  3. Is it a good idea overall?

(it might look similar to this question, but it's not)

回答1:

Your code will be expanded to the following:

public sealed class Singleton
{
    private static readonly Singleton <Instance>k__BackingField = new Singleton();
    public static Singleton Instance { get { return <Instance>k__BackingField; } }
    private Singleton() { /* some initialization code */ }
}

(<Instance>k__BackingField is the unspeakable name of the compiler-generated field.)

So, the properties of your code will be exactly the same as of the code above. Namely, this pattern is thread-safe and it can be a good idea, depending on circumstances.

Assuming you're not accessing any other static members of this type before accessing Instance, then the exact degree of laziness is up to the runtime. Commonly, it will be something like "the instance is created the first time a method that could access Instance is JIT-compiled", but you don't have any guarantee about this.

If you want to make sure that the instance is created just before Instance is accessed for the first time, add an empty static constructor to your class. (This can have a small negative effect on performance, but that probably won't matter to you.)

Since most of this is not really specific to C# 6, a good source of further information would be Jon Skeet's articles about singletons and static constructors/type initializers.