top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is Singleton Design Patterns and How to implement in C#?

0 votes
273 views
What is Singleton Design Patterns and How to implement in C#?
posted Mar 31, 2017 by Sachin

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Singleton Design Pattern-

  1. Ensures a class has only one instance and provides a global point of access to it.

  2. A singleton is a class that only allows a single instance of itself to be created, and usually gives simple access to that instance.

  3. Most commonly, singletons don't allow any parameters to be specified when creating the instance, since a second request of an instance with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter then the factory pattern is more appropriate.)

  4. There are various ways to implement the Singleton Pattern in C#. The following are the common characteristics of a Singleton Pattern.

Some key points:-
 A single constructor, that is private and parameterless.
 The class is sealed.
 A static variable that 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.

Example:

public sealed class Calculate {
 private Calculate() {}
 private static Calculate instance = null;
 public static Calculate Instance {
 get {
 if (instance == null) {
 instance = new Calculate();
 }
return instance;
answer Mar 31, 2017 by Shweta Singh
...