0

This question came up when I was going through a similar question for Java. What is an efficient way to implement a singleton pattern in Java?

Can we implement something similar in C#?

Community
  • 1
  • 1
  • 2
    Check this [thread](http://codereview.stackexchange.com/questions/79/implementing-a-singleton-pattern-in-c) on Code Review – Ufuk Hacıoğulları Dec 04 '13 at 20:11
  • Possibly see: http://msdn.microsoft.com/en-us/library/ff650316.aspx – drew_w Dec 04 '13 at 20:11
  • 3
    whatever you find for java, there is probably a much better, less-code, more beautiful way of doing it in C#. – Federico Berasategui Dec 04 '13 at 20:13
  • @HighCore The standard implementation of a singleton is likely going to look literally identical for both languages...if there is a difference, it's likely to be nominal. – Servy Dec 04 '13 at 20:18
  • http://csharpindepth.com/Articles/General/Singleton.aspx – Habib Dec 04 '13 at 20:20
  • @servy even if it's exactly the same code, with the same lines made up of the same keywords and the same identifiers, C# is better. – Federico Berasategui Dec 04 '13 at 20:23
  • 1
    @HighCore If it's the same, then by definition, it's not better. It is the same. If there were a language feature of C# that made this particular task much more effective than in Java, it might be worth commenting on, but as there isn't, I fail to see the point. – Servy Dec 04 '13 at 20:25
  • 1
    @servy there is no point, don't you see? I'm just kidding... – Federico Berasategui Dec 04 '13 at 20:26

4 Answers4

4

You must have checked Implementing the Singleton Pattern in C# by Jon Skeet

All these implementations share four common characteristics, however:

  • A single constructor, which is private and parameterless. This prevents other > >classes from instantiating it (which would be a violation of the pattern). Note that it >also prevents subclassing - if a singleton can be subclassed once, it can be subclassed >twice, and if each of those subclasses can create an instance, the pattern is violated. The factory pattern can be used if you need a single instance of a base type, but the exact type isn't known until runtime.
  • The class is sealed. This is unnecessary, strictly speaking, due to the above point, but may help the JIT to optimise things more.
  • A static variable which holds a reference to the single created instance, if any.
  • A public static means of getting the reference to the single created instance, creating one if necessary.
public class Singleton 
{
    static readonly Singleton _instance = new Singleton();

    static Singleton() { }

    private Singleton() { }

    public static Singleton Instance
    {
        get  { return _instance; }
    }
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

common way to implement one

public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();

    private Singleton() {}
    static Singleton() {}

    public static Singleton Instance { get { return instance; } }
}
Konstantin
  • 3,254
  • 15
  • 20
1
public class Singleton
 {
        private static Singleton instance = null;

        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Singleton ();
                }
                return instance;
            }
        }
tdelepine
  • 1,986
  • 1
  • 13
  • 19
0

I found a few for you:

Jon Skeet's.

This post.

And this one as well.

At the end of the day though, these are all pretty subjective so, you need to do what is best for you and what you are trying to accomplish.

Community
  • 1
  • 1
Brian
  • 5,069
  • 7
  • 37
  • 47