C#中实现单实例


public class SingletonDemo
{
    private static SingletonDemo theSingleton = null;
    private SingletonDemo() { }
    public static SingletonDemo Instance()
    {
        if (theSingleton == null)
        {
            theSingleton = new SingletonDemo();
        }
        return theSingleton;
    }
    static void Main(string[] args)
    {
        SingletonDemo s1 = SingletonDemo.Instance();
        SingletonDemo s2 = SingletonDemo.Instance();
        if (s1.Equals(s2))
        {
            Console.WriteLine("see, only one instance!");
        }
    }
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注