Program to write a Singleton Pattern using Nested Class in C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 1402
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();
}
}
}
}
}

Comments or Responses

Login to post response