Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.
mr-ponna.com

 

YOU ARE HERE: HOME Questions How to implement singleton design pattern in C



Singleton design pattern in C#

View(s): 28426

How to implement singleton design pattern in C#?

Answer 1)

The intent of the Singleton pattern as defined in Design Patterns is to "ensure a class has only one instance, and provide a global point of access to it".

The singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects (say, five). Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application.

Below is sample implementation:

/// <summary>
/// Thread-safe singleton example created at first call
/// </summary>
public sealed class Singleton
{
    private static readonly Singleton _instance = new Singleton(); 

    private Singleton() { } 

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

I recommend reading the article "Implementing the Singleton Pattern in .NET" to choose best implementation.

  Asked in:  TCS (Tata Consultancy Services), iGATE, Mahindra Satyam, Wipro, MindTree, ValueLabs, Kenexa   Expertise Level:  Experienced
  Last updated on Friday, 13 July 2012
3/5 stars (11 vote(s))

Register Login Ask Us Write to Us Help