how to implement singelton in C# & its uses?



how to implement singelton in C# & its uses?..

Answer / 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

More C Sharp Interview Questions

If you want to convert a base type to a derived type, what type of conversion do you use?

0 Answers  


What?s the advantage of using System.Text.StringBuilder over System.String?

3 Answers  


Define clr in .net?

0 Answers  


Why do we use namespace in c#?

0 Answers  


Can a constructor be private in c#?

0 Answers  






what is meant inheritance. can you exaplain what kind inhertance ussed in your project

1 Answers  


How can you write a class to restrict that only one object of this class can be created (Singleton class)?

0 Answers  


How can I create image pieces/sub image?

0 Answers  


What does mean before a string in c#?

0 Answers  


What is the difference between Java and .NET garbage collectors?

0 Answers   BirlaSoft,


What are iterators?

5 Answers   TCS,


Is c# an open source language?

0 Answers  


Categories