天天看点

c#中单例的实现

#region "实现这个窗口类的单例,单例类常用于被主窗口以show()方法打开的窗口,使用单例后只会打开一个此类的对象"
        //1.私有化构造函数,使在外部不能new(开辟堆空间,创建对象,调用构造函数)
        private FStudentMan()
        {
            InitializeComponent();
        }
        //2.创建一个静态的私有的窗体类的变量 
        private static FStudentMan single;
        //3.创建一个静态的公共的方法返回窗体类对象
        public static FStudentMan GetSingle()
        {
            if (single == null||single.IsDisposed)
            {
                single=new FStudentMan();
            }
            return single;
        }

#endregion