how to implement singelton in C# & its uses?

Answer Posted / chandana

To implement a singleton in C#, the steps are:

1. Make the constructor private
2. Declare a static object of the same class
3. Define a static method which return the same object.
This method checks the static variable object, if it
exists, return the same, if it doesn't exist, then creates
a new instance and returns.

This is used when we want to limit the object creation to
only one object for a particular class. e.g., For a
company, we would like to limit the object of type CEO to
be only one. (that means, we know that only 1 CEO exists
for a company).

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Value Type and Reference Type Data type in C#?

624


What does public mean in c#?

600


Why do canadians say zed?

637


Why do we need nullable types in c#?

772


Why do we use abstraction in c#?

553






What is the difference between “out” and “ref” parameters in c#?

592


What is the difference between delegates and events in c#?

511


What is xml document how do you open it?

645


To allow an element to be accessed using a unique key which .NET collection class is used ?

712


Why do we need delegates in c#?

597


Why delegates are type safe?

596


What is a scope in c#?

565


what are windows services?

602


How can we sort the elements of the array in descending order?

615


What is meant by enumerable in c#?

580