月度归档:2010年11月

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!");
        }
    }
}