Singleton Design pattern?How do you achieve it?

Answer Posted / sudhir sheoran

Singleton design pattern can be achieved by following implementation:

1) Make class public

2) Static method to generate the instance of class(calling this method we can get the instance of a class if already a instance is generated we will get the same instance)

3) Private Constructor (so that no other class can generate the instance of singleton class)

Example::

class Class1
{
private static Class1 classInstance;
private IList<int> list = new List<int>();
private Class1()
{
if (list == null)
{
list = new List<int>();
}

}

public static void GetInstance()
{
if (classInstance == null)
{
classInstance = new Class1();
}

}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is app_code folder in asp net?

767


What are the Types of session management in ASP.NET

780


How to deploy/publish webservices?How many ways?Plz explain me

2065


Does asp.net still recognize the global.asa file?

743


What is a postback ispostback and autopostback in asp net?

680


What is web router?

696


Describe the difference between inline and code behind - which is best in?

788


Explain login control and form authentication.

738


Can we have multiple web config files for an asp.net application?

739


What is parse in asp.net?

767


When maintaining session through sql server, what is the impact of read and write operation on session objects?

786


What are resource file and how do we generate resource file?

785


How can we create custom controls in asp net?

770


Are cookies stored on server or client?

746


Explain diff between dataset and datareader?

731