下面的例子描述了delegate的声明,初始化和调用:
public partial class Form1 : Form
{
// declares a delegate, it has one parameter.
delegate void help(string message);
public Form1()
{
InitializeComponent();
// anonymous method.
this.MouseClick += delegate(object sender, MouseEventArgs e)
{
MessageBox.Show("hello world");
};
}
private void Form1_Load(object sender, EventArgs e)
{
// 2 ways to initialize the delegate
// 1.decides what to do with this delegate, here using a named method "ShowMessage".
help h1 = new help(Form1.ShowMessage); // also we can write it as "help h1 = Form1.ShowMessage", they are the same meaning.
// 2.decides what to do with this delegate, here using a anonymous method.
help h2 = delegate(string message) { MessageBox.Show(message); };
// 2 ways to call the delegate
// 1.call the delegate
h1.Invoke("message from delegate1");
// 2.call the delegate
h2("message from delegate2");
}
private static void ShowMessage(string message)
{
MessageBox.Show(message);
}
}
接下来总结一下常用的事件的注册方法,这里我列出了四种方式:
Code
public partial class Form1 : Form
{
// 1. delegate
public delegate void TestEvent1Handler(object sender, EventArgs e);
public event TestEvent1Handler TestEvent1;
// 2. delegate with parameter
public delegate void TestEvent2Handler(string para);
public event TestEvent2Handler TestEvent2;
// 3. EventHandler
public event EventHandler TestEvent3;
// 4. EventHandler with customized eventargs
public event EventHandler<MyEventArgs> TestEvent4;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 1.
if (TestEvent1 != null)
TestEvent1(sender, e);
// 2.
if (TestEvent2 != null)
TestEvent2("new name from 2");
// 3.
if (TestEvent3 != null)
TestEvent3(sender, e);
// 4.
if (TestEvent4 != null)
TestEvent4(this, new MyEventArgs("new name from 4"));
}
}
/// <summary>
/// Customized EventArgs.
/// </summary>
public class MyEventArgs : EventArgs
{
string name;
public MyEventArgs(string name)
{
this.name = name;
}
public string Name { get { return name; } }
}
注册这些事件如下:
Code
class Class1
{
public Class1()
{
Form1 form = new Form1();
form.TestEvent1 += new Form1.TestEvent1Handler(form_TestEvent1);
form.TestEvent2 += new Form1.TestEvent2Handler(form_TestEvent2);
form.TestEvent3 += new EventHandler(form_TestEvent3);
form.TestEvent4 += new EventHandler<MyEventArgs>(form_TestEvent4);
form.Show();
}
void form_TestEvent4(object sender, MyEventArgs e)
{
MessageBox.Show("Test event 4: " + e.Name);
}
void form_TestEvent3(object sender, EventArgs e)
{
MessageBox.Show("Test event 3");
}
void form_TestEvent2(string para)
{
MessageBox.Show("Test event 2: " + para);
}
void form_TestEvent1(object sender, EventArgs e)
{
MessageBox.Show("Test event 1");
}
}
下面介绍三个常用的delegate:Func,Action和Predicate
1. Func<T,TResult> delegate:
语法:public delegate TResult Func<T, TResult>(T arg);
用它封装一个方法,该方法只有一个参数,T是参数的类型,TResult是返回值的类型。
例如:
Code
private void Test()
{
// 1. normal
Func<string, string> d1 = ToUpper;
// 2. anonymous method
Func<string, string> d2 = delegate(string s) { return s.ToUpper(); };
// 3. lambda expression
Func<string, string> d3 = s => { return s.ToUpper(); };
Func<string, string> d4 = s => s.ToUpper();
Console.WriteLine(d1("china"));
Console.WriteLine(d2("china"));
Console.WriteLine(d3("china"));
Console.WriteLine(d4("china"));
}
string ToUpper(string s)
{
return s.ToUpper();
}
2. Action<T>, Action<T1, T2>,Action 等等
它封装一个方法,带有一个或多个参数,但是没有返回值。
它与Func的区别就是Func有返回值,Action无返回值。
3. Predicate<T>
predicate的意思是断定为,预言成..., 它封装了一个方法,带有一个参数,返回值是bool型。