dimanche 28 mai 2017

Does two instance of singleton class has a same property value?

Hello stack in new to design pattern in c# can any one please give me some instruction about implementation of singleton class. i just implement one tutorial but i don't able to exact use of singleton class with this "singleton means we can create only one instance of a class" then why we don't access property which is written in the singleton class using two different instance of the class.

please look my code and give me instruction what was the mistake i made.

static void Main(string[] args)
    {

        Singleton instance = Singleton.getInstance();
        instance.Message = "Text Message";

        Singleton instance1 = Singleton.getInstance();
        Console.WriteLine(instance.Message);
        Console.WriteLine(instance1.Message);
        Console.ReadKey();
    }
class Singleton
{
    private static Singleton singleton=null;
    private Singleton(){}
    public static Singleton getInstance()
    {
        if (singleton!=null)
        {
            return singleton;
        }
        return new Singleton();
    }
    public string Message{get; set;}
}

Aucun commentaire:

Enregistrer un commentaire