Example of Singleton Design Pattern in C#
Please Subscribe Youtube| Like Facebook | Follow Twitter
In this tip we will Implement Singleton Design Pattern in our code.
public sealed class Singleton
{
private static Singleton obj;
public static Singleton Instance() {
if (obj == null) {
obj = new Singleton();
}
return obj;
}
}
Thread Safe:
public sealed class Singleton
{
private static Singleton obj;
private static readonly object padlock = new object();
public static Singleton Instance()
{
get
{
lock (padlock)
{
if (obj == null)
{
obj = new Singleton();
}
return obj;
}
}
}
}
Please Subscribe Youtube|
Like Facebook |
Follow Twitter