Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-property initializer Singleton implementation

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)

like image 754
Alice Avatar asked Sep 23 '15 11:09

Alice


People also ask

What is Auto Property Initializers?

Auto-property initializers are basically a shortcut to setting the property value in the constructor. I wasn't overly excited about the new feature at first, but I think it makes the intention a lot more clear when you see the initial value on the same line as the auto-implemented property.

What is an efficient way to implement a singleton pattern in Java?

The most popular approach is to implement a Singleton by creating a regular class and making sure it has: A private constructor. A static field containing its only instance. A static factory method for obtaining the instance.


1 Answers

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.

like image 140
svick Avatar answered Sep 17 '22 16:09

svick