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 are the Types of object in asp

633


Describe the diffeerence between inline and code behind?

507


What’s the catch?

617


What is inheritance and an how it be used, example with an example?

582


What are the features of asp net?

510






Explain one critical mapping?

560


What is the difference between pathparam and queryparam?

571


What are client activated objects and server activated objects?

512


What is a windows service?

553


What is is post back property in asp net?

532


What is the difference between typeof() vs gettype()?

549


If 200 is for all successful operation then why do we have 201 response codes?

522


What is global.asax file used for?

553


What is razor? : asp.net mvc

553


Explain code snippet to register exception filters from controller?

621