Singleton Design pattern?How do you achieve it?
Answer Posted / sudhir sheoran
Singleton design pattern can be achieved by following implementation:
1) Make class public
2) Static method to generate the instance of class(calling this method we can get the instance of a class if already a instance is generated we will get the same instance)
3) Private Constructor (so that no other class can generate the instance of singleton class)
Example::
class Class1
{
private static Class1 classInstance;
private IList<int> list = new List<int>();
private Class1()
{
if (list == null)
{
list = new List<int>();
}
}
public static void GetInstance()
{
if (classInstance == null)
{
classInstance = new Class1();
}
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
How to automatically get the latest version of all the asp.net solution items from source safe when opening the solution?
What are the ways to show data grid inside a data grid for a master details type of tables? If we write any code for DataGrid methods, what is the access specifier used for that methods in the code behind file and why?
What are the asp.net list controls and difference between them?
What is data binding in asp net?
What does the orientation property do in a menu control?
How we can force all the validation controls to run?
Define xmlvalidatingreader class.
Why is global asax is used?
In What Order Do The Events Of An Aspx Page Execute. As A Developer Is It Important To Undertsand These Events?
What is caching? What are different ways of caching in asp.net?
What is preprocessor in .net and type, where it use?
Can I read the hard disk serial # of the client computer using asp.net?
How can we create Tree control in asp.net?
Define static function?
What are the elements of a website?