public partial class Form1 : Formthis
{spa
public Form1()orm
{string
InitializeComponent();it
}io
public delegate void showMessage(string msg);table
public static void showM(string m)class
{object
MessageBox.Show(m);foreach
}
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>委託出來的結果"));
}
static void Main(string[] args)
{
IList<String> list = new List<String>();
list.Add("1");
list.Add("2");
list.Add("3");
list.Add("4");
list.Add("5");
Func<String, bool> f = x => x.Equals("2");
IList<String> list2 = filter(f, list);
}
static IList<String> filter(Func<String, bool> exp, IList<String> list)
{
return list.Where(exp).ToList();
}
}