Regarding to many Requests to Talk about Design Pattern at .NET.
We will start today our Series so stay in touch for upcoming Posts.
Today we will talk about Singleton.
- It comes under creational pattern.
- Ensure a class has only one instance and provide a global point of access to it.
- It’s Easy.
Code
publicclasssingleton
{
// Let the core .NET Framework Manages the locking, mutex and volatile stuff of you!
//Because Static Keyword initilized only one time...
privatestaticreadonlysingleton instance = newsingleton();
private singleton()
{
Console.WriteLine("Singleton Constructor! ");
}
publicstaticsingletoncreateSingleton
{
get
{
return instance;
}
}
publicvoiddosomething()
{}
}