Singleton template - simplified implementation?

The singleton template implementation proposed in C # in depth ,

public sealed class Singleton { private static readonly Singleton instance = new Singleton(); static Singleton() { } private Singleton() { } public static Singleton Instance { get { return instance; } } } 

ReSharper suggests simplifying this by using the auto property and the C # 6 auto-source initializer:

 public sealed class Singleton { static Singleton() { } private Singleton() { } public static Singleton Instance { get; } = new Singleton(); } 

It really looks easier. Is there any way to use this simplification?

+8
c # singleton resharper
source share
1 answer

On the site https://sharplab.io you can see the IL code, in both cases the IL code is similar. Thus, this should work the same.

+2
source share

All Articles