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 string interpolation in c#?
What is an extension method in c#?
How does a while loop work?
Can you pass value types by reference to a method?
What is argument in c#?
What are the advantages of using delegates in c#?
what are the Disadvantages of vb
How can I get the ascii code for a character in c#?
Explain the access modifiers in c#?
What is application c#?
Why do we need generics?
Which types of inheritances does c# support?
How is the syntax written to inherit from a class in C#?Give an example ?
What is expression c#?
What is difference between override and new in c#?