//以上委托声明可以改为无返回值Action<T1...T16> T代表参数最多16个
private void button1_Click(object sender, EventArgs e)
{
Action R = () => Run();//声明和指向(隐藏了实例化)
a.Invoke();
frm2.show();//如果有窗体显示一定放到最后.不然有异步不显示frm2.Textbox
}
public void Run()
{
textBox1.Text = "13534";
}
//有参方式
private void button1_Click(object sender, EventArgs e)
{
Action<string> R =m => Run(textBox1.Text);//声明和指向(隐藏了实例化)
R.Invoke("");
}
public void Run(string msg )
{
textBox1.Text = "结果"+msg;
}
//泛型多播委托
Action<string> R = x => Avoid("1");
R += Bvoid;
R.Invoke("2");
public void Avoid(string s)
{
MessageBox.Show("A"+s.ToString());
}
public static void Bvoid(string s)
{
MessageBox.Show("B" + s);
}
//Func<out TResult> <返回值>
//Func<in T,out TResult> <参数,返回值>
//Func<in T1,in T2,...,in T16, out TResult> <参数1,参数2...,返回值>
//无参
private void button1_Click(object sender, EventArgs e)
{
Func<string> R =() => Run();//声明和指向(隐藏了实例化)
R.Invoke();
}
public string Run()
{
return textBox1.Text = "结果" ;
}
//有参
private void button1_Click(object sender, EventArgs e)
{
Func<int,string> R =v => Run(12);//Func<参数,返回值>
R.Invoke(0);
}
public string Run(int Msg)
{
return textBox1.Text = "结果" + Msg.ToString();
}
//多参
private void button1_Click(object sender, EventArgs e)
{
//原型:Func<string, bool, DataTable> R = new Func<string, bool, DataTable>(ds);
Func<string, bool, DataTable> R = (string s, bool b) => ds("Lambad", true);
dt = R.Invoke(null, true);
MessageBox.Show(dt.Rows[0][0].ToString());
}
private static DataTable ds(string a,bool f=true)
{
DataTable dt=new DataTable();
dt.Columns.Add("A");
DataRow dr = dt.NewRow();
dr[0] = a;
dt.Rows.Add(dr);
return dt;
}
//替代函数
public Func<string, int> Len = x =>
{
return x.Length;
};
//实例应用
//原型 public static Func<string,bool,DataTable> R =(string m,bool b) =>{};
public static Func<string,bool, System.Data.DataTable> R =(m,b) =>
{
System.Data.DataTable dt = new System.Data.DataTable();
Scom = new SqlCommand(m, Scon);
Sadp = new SqlDataAdapter();
Sadp.SelectCommand = Scom;
Sadp.Fill(dt);
return dt;
};
//上例原型
public static System.Data.DataTable Run(string sql, bool flag = true)
{
System.Data.DataTable dt = new System.Data.DataTable();
Scom = new SqlCommand(sql, Scon);
Sadp = new SqlDataAdapter();
Sadp.SelectCommand = Scom;
Sadp.Fill(dt);
return dt;
}
//用委托和拉姆达表达式替代函数
public Func<string, int> Len = x =>
{return x.Length; };
//简化
Func<string, object> Lenn = m => MessageBox.Show(m.Length.ToString());
//无参方式
private delegate void DeleString();//定义委托
Form2 frm2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
DeleString a = () => Run();//无参()
a.Invoke();
frm2.show();//如果有窗体显示一定放到最后.不然有异步不显示frm2.Textbox
}
public void Run()
{
textBox1.Text = "13534";
}
//有参方式
private delegate void DeleString(string Msg);//定义委托
private void button1_Click(object sender, EventArgs e)
{
DeleString a = x => Run(textBox1.Text);
a.Invoke("");
}
public void Run(string msg )
{
textBox1.Text = "结果"+msg;
}
//多播委托
private delegate void DelCr(string s);
private void button1_Click(object sender, EventArgs e)
{
DelCr R = new DelCr(Avoid);
R += Bvoid;
R.Invoke("sd");
}
public void Avoid(string s)
{
MessageBox.Show("A"+s.ToString());
}
public static void Bvoid(string s)
{
MessageBox.Show("B" + s);
}