public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void showMessage(string msg);
public static void showM(string m)
{
MessageBox.Show(m);
}
public string returnM(string m)
{
return m;
}
private void button1_Click(object sender, EventArgs e)
{
//3
showMessage s = new showMessage(showM);
s("xxx");
//1
Action<string> action = showM;
action("123");
//2
Action<string> a = X => showM(X);
a("aaaaaaaaaa");
//6
Action<string> ac = (x1) =>
{
MessageBox.Show(x1);
};
ac("111111111111111");
//6
Action<string> ac2 = delegate(string xx){
MessageBox.Show(xx);
};
ac2("222222222222");
//5
string m1 = "123";
textBox1.Invoke((Action<string>)delegate(string m)
{
textBox1.Text = m;
},m1);
textBox1.Invoke(new Action<string>((x4) =>
{
textBox1.Text = x4;
}), "aaaaax4");
//4 --需要return
Func<string, string> f = returnM;
MessageBox.Show(f("abc"));
//7
Func<string> func = delegate()
{
return "我是Func<TResult>委托出来的结果";
};
MessageBox.Show(func());
//8
Func<string, string> funcOne = delegate(string s3)
{
return s3;
};
MessageBox.Show(funcOne("我是Func<T,TResult>委托出来的结果"));
//9
Func<string, string, string> funcTwo = delegate(string value1, string value2)
{
return value1 + " " + value2;
};
MessageBox.Show(funcTwo("xxx", "我是Func<T,TResult>委托出来的结果"));
//10
Func<string, string, string> ff = (x1, x2) =>
{
return x1 + " " + x2;
};
MessageBox.Show(funcTwo("xxx", "我是Func<T,TResult>委托出来的结果"));
}
}