adspace


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

How to assign Null value to Var?

1073


What is expression tree in c#?

1007


Why can't we use a static class instead of singleton?

963


Which namespaces are necessary to create a localized application?

1151


What is an abstract class c#?

978


How do you inherit a class into other class in c#?

1001