Auto-property initializer Singleton implementation

断了今生、忘了曾经 提交于 2019-12-06 22:46:44

问题


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.



来源:https://stackoverflow.com/questions/32738283/auto-property-initializer-singleton-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!