一、委托
callback 回调函数
声明委托:(与声明类相似)
public delegate string MyDelegate(string sInput);
使用委托:
1. 
MyDelegate myDelegateCase = new MyDelegate(InvokeMethod);
private string InvokeMethod(string sInput)
{
   return sInput);
}
2. 委托推断
MyDelegate myDelegateCase = InvokeMethod;
3. 匿名方法
MyDelegate myDelegateCase = delegate(string sInput){ return sInput; };
多播委托
MyDelegate myDelegateCase = InvokeMethod1;
myDelegateCase += InvokeMethod2;
委托总是带有1个参数的构造函数,这个参数就是委托引用的方法。
二、事件
EventHandler 1 总是返回void,2 参数必须是object、EventArg
声明委托和事件:
public delegate void ActionEventHandler(object sender, ActionCancelEventArgs ev);
public static event ActionEventHandler Action;
定义引发事件:
protected void OnAction(object sender, ActionCancelEventArgs ev)
{
    if (Action != null)
    {
        Action(sender, ev);
    }
}
Class.Action += new ActionEventHandler(MainForm_Action);
private void MainForm_Action(object sender, ActionCancelEventArgs e)
{
}
最后调用OnAction引发事件
posted on 2011-04-07 21:54  huadust  阅读(216)  评论(0编辑  收藏  举报