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

Is stringbuilder thread safe c#?

643


What is thread pooling?

738


When was .net linq added?

730


What do you mean by delegates and explain different types of delegates?

728


what is a structure in c#

1032


In a single .NET DLL how many classes it contains?

718


What is the difference between double and decimal in c#?

693


What is the difference between namespace and class?

658


What is expression tree with example?

663


What are the 2 kinds of data type conversions in c#?

685


What is the function of the not null constraint?

709


Different between method overriding and method overloading?

684


What is dataadapter c#?

658


What are the advantages of using delegates in c#?

669


What is the adv of using System.Text.StringBuilder over System.String?

741