本帖最后由 许庭洲 于 2013-5-11 06:16 编辑
1.委托是类型;
2.事件是对象;
3. 事件的内部是用委托实现的。
举例子:
// Declare a delegate delegate void Printer(string s); class TestClass { staticvoid Main() { //Instatiate the delegate type using an anonymous method: Printer p = delegate(string j) { System.Console.WriteLine(j); }; //Results from the anonymous delegate call: p("The delegate using the anonymous method is called."); //The delegate instantiation using a named method "DoWork": p= new Printer(TestClass.DoWork); //Results from the old style delegate call: p("The delegate using the named method is called."); } // Themethod associated with the named delegate: static void DoWork(string k) { System.Console.WriteLine(k); } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
输出 The delegate using the anonymous method iscalled. The delegate using the named method is called.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|