I am in the process of understanding the singleton pattern. I have written a small piece of code here
Program.cs:
class Program
{
static void Main(string[] args)
{
SingleObject objtemp = SingleObject.getInstance();\
objtemp.showMessage();
}
}
SingleObject.cs
class SingleObject
{
static SingleObject obj = new SingleObject();
private SingleObject()
{
}
public static SingleObject getInstance()
{
return obj;
}
public void showMessage()
{
Console.WriteLine("Hello Message");
}
}
I am not able to understand what is actually calling the SingleObject()
constructor?
When I call getInstance()
method is it returning the instance correctly?