C#单例模式

//全局唯一的单例
public static Form2 FrmSingle=null;

private Form2()
{
InitializeComponent();
}

public static Form2 GetSingle()
{
if (FrmSingle == null)
{
FrmSingle = new Form2();
}
return FrmSingle;




 

 

 1 namespace _TEST_
 2 {
 3 public partial class Form1 : Form
 4 {
 5 public Form1()
 6 {
 7 InitializeComponent();
 8 }
 9 
10 private void button1_Click(object sender, EventArgs e)
11 {
12 Form2 frm2 = Form2.GetSingle();//new Form2();
13 frm2.Show();
14 }
15 }
16 }

 

posted @ 2021-05-22 18:37  Nanyingming  阅读(40)  评论(0编辑  收藏  举报