The below program will create a Singleton Pattern using Nested Class concept in C#
namespace ConsoleApplication1
{
public sealed class Singleton
{
private static Singleton instance = null;
private static readonly object lockObj = new object();
Singleton(){} //private constructor
public static Singleton Instance
{
get
{
lock (lockObj)
{
if (System.Object.ReferenceEquals(instance, null))
{
instance = Inner.Instance;
}
return instance;
}
}
}
class Inner
{
Inner(){ }//private constructor
public static Singleton Instance
{
get
{
return new Singleton();
}
}
}
}
}