How to create a class which can only have a single instance in C#

32,649

Solution 1

Using singleton, that is a class which only allows a single instance of itself to be created.

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

The operation of this pattern is simple and could be reduced to the following:

Hide the constructor of the Singleton class, so that clients may not be instantiated. To declare the Singleton class private member variable containing the reference to the unique instance that we handle. Provide in class Singleton a function or property that provides access to the one maintained by the Singleton instance.

Solution 2

this can be sample Singletone implementation

public sealed class CSingleTone
{
    private static CSingleTone instance;
    public int SomeValue{ get; set; }
    public static CSingleTone Instance
    {
        get
        {
            if (instance == null)
                instance = new CSingleTone();
            return instance;
        }
    }
    private CSingleTone()
    {
    }
}

can be used in this way

int r = CSingleTone.Instance.SomeValue;

Solution 3

You need to read our own Jon Skeet's blog (Implementing the Singleton Pattern in C#).

Intent of Singletom Pattern is to "ensure a class has only one instance, and provide a global point of access to it".

Here's how you can do it.

public sealed class Singleton {

    private static readonly Singleton instance = new Singleton();

    private Singleton() {       
    }

    public static Singleton Instance
    {
        get 
        {
            return instance;
        }
    }    
}

Solution 4

Singleton is not a class, but rather a pattern. Here is an example:

class Singleton {
    private Singleton() {}

    static private Singleton GetIt() {
        return theOne;
    }

    static private Singleton theOne = new Singleton();
}

Solution 5

here is a simple example of singleton class

class Program
{
    static void Main()
    {
    SiteStructure s = SiteStructure.Instance;
    }
}

public sealed class SiteStructure
{
    static readonly SiteStructure _instance = new SiteStructure();
    public static SiteStructure Instance
    {
    get
    {
        return _instance;
    }
    }
    SiteStructure()
    {
    // Initialize.
    }
}

here, Readonly allows thread-safety, and that means it can be only allocated once. It has a public static getter. The Instance property is used by callers to get the singleton instance. Sealed is known to allow the compiler to perform special optimizations during JIT compilation. The final methods above are the private instance constructor and an Initialize method. Private constructors mean the class can only allocate itself.

Share:
32,649
Tomas Ramirez Sarduy
Author by

Tomas Ramirez Sarduy

Frontend engineer at @DAZN. I'm pixel perfectionist, performance obsessed and Chrome Developer Tools fan. When I’m not coding or fixing a bug, I'm probably here.

Updated on July 09, 2022

Comments

  • Tomas Ramirez Sarduy
    Tomas Ramirez Sarduy almost 2 years

    I wonder if there is a mechanism or pattern to allow only one instance of a class in C#. I have heard of the Singleton class, but i don't know how to use it well.

  • Admin
    Admin almost 13 years
    Are there any issue(s) with MT caused by being non-readonly? (I do not know here.)
  • Petar Ivanov
    Petar Ivanov almost 13 years
    Not really.theOne is private so doesn't matter.
  • Tomas Ramirez Sarduy
    Tomas Ramirez Sarduy almost 13 years
    And if I would have at most n instances of the class instead one? (curiosity)
  • crenshaw-dev
    crenshaw-dev almost 6 years
    This answer may predate newer syntax... but would public static Singleton Instance { get; } = new Singleton(); do the trick, avoiding the private readonly field?