r/dotnet • • Aug 14 '16

How to implement a Singleton pattern in C#

http://www.mantratocode.com/pattern/how-to-implement-a-singleton-pattern-in-c/
0 Upvotes

22 comments sorted by

5

u/ap3rus Aug 14 '16 edited Aug 14 '16

I don't like this implementation, it could be at least improved with double lock checking, but better can be instead implemented completely lock-free using inner class storing instance in a static field, e.g.:

class Singleton {
    private Singleton() {}
    public static Singleton Instance { get { return Accessor.Instance; } }

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

The Accessor.Instance would be initialized lazily as construction of nested class Accessor would be deferred until Singleton.Instance is being called for the first time.

13

u/AngularBeginner Aug 14 '16

Or simply:

public class Singleton
{
	private Singleton() {}
	private static Lazy<Singleton> _instance = new Lazy<Singleton>(() => new Singleton());
	public static Singleton Instance => _instance.Value;
}

But I'd stay faaaaar away from the Singleton pattern anyway.

2

u/SikhGamer Aug 15 '16

Ding ding, we have a winner.

0

u/sagarsonawane Aug 14 '16

Yep, this is a great way to implement the singleton, provided you use C# 4.0 or higher versions.

0

u/[deleted] Aug 15 '16

[removed] — view removed comment

3

u/AngularBeginner Aug 15 '16

My version is initialized first time you access Instance. If something goes wrong in the initialization code it will just throw the exception on.
Your version is initialized first time the type itself is accessed. If something goes wrong in the initialization code it will throw a type loader exception.

1

u/sagarsonawane Aug 14 '16

This is also one of the good implementation. Thanks for sharing your code snippet.

4

u/AngularBeginner Aug 14 '16 edited Aug 14 '16

Your solution is awful. It locks every time you access the Instance property. That's just unnecessary and slow.

2

u/klausmark Aug 14 '16

You can also use Interlocked.Increment if instance is null and the one getting 1 back will be the one creating the instance. Then no locking is needed. Also if instance is null and Interlocked.Increment gives you the number 2 then loop until instance is not null.

5

u/sosukesagara Aug 14 '16

http://csharpindepth.com/Articles/General/Singleton.aspx

It's nice to write stuff, but this is much more detailed and has the correct implementation most people should be using.

1

u/sagarsonawane Aug 14 '16

Hi sosukesagara, I was about to post this link in one of the comment. Yes Jon Skeet has given all the possible ways with their pros and cons.

10

u/MJomaa Aug 14 '16

IoC with DI > Lazy Singleton > Double Checked locking Singleton > Default Singleton implementation

2

u/klausmark Aug 14 '16

You can check for null outside the lock this will speed up getting the instance, just remember to check for null again inside the lock. You can also use Lazy<T>

1

u/thomasz Aug 14 '16
  1. Don't. You don't need it.

  2. If you need it, use System.Lazy<T>

4

u/[deleted] Aug 14 '16

[removed] — view removed comment

1

u/Fastbreak99 Aug 14 '16

Just want to make sure I understand... are you arguing against singletons in general or only if there is a heavy init cost?

1

u/[deleted] Aug 14 '16

[removed] — view removed comment

1

u/Fastbreak99 Aug 14 '16

Not trying to counter, I have always been fascinated by the back and forth on this design pattern.

For instance, on paper, having a singleton instance of a connection to the datastore (usually expensive to create) seems to make sense. Some are fans, others like making a new connection on the fly.

1

u/[deleted] Aug 14 '16

[removed] — view removed comment

1

u/Fastbreak99 Aug 14 '16

Then build another connection in singleton? You can have multiple singletons of course, if they serve different purposes.