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 postback request?

774


How do you secure your connection string information?

759


What is hidden field in asp.net?

818


How we implement web farm and web garden concept in asp.net?

786


How do session tokens work?

753


Which namespace is used by ado.net?

784


List some of the important session state modes of asp.net.

750


What are the advantages and disadvantages of session?

903


How many types of session state management options available in asp.net?

822


What is server side session?

769


How do cookies work? Give an example of their abuse.

781


How can we make sure that Web API returns JSON data only?

812


How do u optimize a query in asp.net?

805


What is difference between web api and web services?

784


What are Caching techniques in .NET

847