The singleton pattern implementation suggested in C# in Depth is
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
ReSharper suggests to simplify this using an auto property and the C# 6 auto-property initializer:
public sealed class Singleton
{
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance { get; } = new Singleton();
}
This does indeed look simpler. Is there a down-side to using this simplification?
On site https://sharplab.io you can look at IL code, in both cases IL code are similar. So this should work same way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With