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

What is task parallel library?

741


What is entity framework in c#?

685


how to implement a web service in .net

718


Compare and contrast between the System.Array.CopyTo() and Clone()?

778


What are "class access modifiers" in C#?

746


In .NET how can you solve the DLL Hell problem?

887


What is the different types of private assembly and shared assembly?

732


Can an abstract class have a constructor c#?

701


In howmany ways can you deploy an assembly?

733


Which types of inheritances does c# support?

672


What is string interpolation in c#?

739


Why singleton pattern is used in c#?

728


Why data types are important in c#?

704


Why do we use interface in c#?

708


Is c# an open source language?

771