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
What is task parallel library?
What is entity framework in c#?
how to implement a web service in .net
Compare and contrast between the System.Array.CopyTo() and Clone()?
What are "class access modifiers" in C#?
In .NET how can you solve the DLL Hell problem?
What is the different types of private assembly and shared assembly?
Can an abstract class have a constructor c#?
In howmany ways can you deploy an assembly?
Which types of inheritances does c# support?
What is string interpolation in c#?
Why singleton pattern is used in c#?
Why data types are important in c#?
Why do we use interface in c#?
Is c# an open source language?