How to use Singleton pattern

Main theme of Singleton Pattern is there will be only one instance of a class. In the below example there is no way to create multiple instance of the class SingletonPatternTest

public class SingletonPatternTest
static SingletonPatternTest oSingletonPatternTest;

public int Count

private SingletonPatternTest()


public static SingletonPatternTest GetInstance()
if (oSingletonPatternTest == null)
oSingletonPatternTest = new SingletonPatternTest();

return oSingletonPatternTest;

 SingletonPatternTest oSingletonPatternTest  = SingletonPatternTest.GetInstance(); /*This always returns the same instance*/


 SingletonPatternTest oSingletonPatternTestNew  = SingletonPatternTest.GetInstance();

Response.Write(“Count”+oSingletonPatternTestNew.Count.ToString()); /*This will write count value as 10*/


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: