命名的方法 匿名方法 对委托进行实例化
2007-07-26 11:58
363 查看
命名方法对委托实例 // Declare a delegate delegate void Del(int i, double j); class MathClass { static void Main() { MathClass m = new MathClass(); // Delegate instantiation using "MultiplyNumbers" Del d = m.MultiplyNumbers; // Invoke the delegate object. System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':"); for (int i = 1; i <= 5; i++) { d(i, 2); } } // Declare the associated method. void MultiplyNumbers(int m, double n) { System.Console.Write(m * n + " "); } }
匿名方法与命名方法 实例 比较
[code]// Declare a delegate delegate void Printer(string s); class TestClass { static void 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."); } // The method associated with the named delegate: static void DoWork(string k) { System.Console.WriteLine(k); } }
[/code]
输出 [code]The delegate using the anonymous method is called.
The delegate using the named method is called.
[/code]
相关文章推荐
- 带有命名方法的委托与带有匿名方法的委托(C# 编程指南)
- 带有命名方法的委托与带有匿名方法的委托
- 带有命名方法的委托与带有匿名方法的委托
- 带有命名方法的委托与带有匿名方法的委托
- 使用委托,匿名方法对集合进行万能排序
- 探讨:如何使用委托,匿名方法对集合进行万能排序
- C#中分别对委托、匿名方法、Lambda表达式、Lambda表达式树以及反射执行同一方法的过程进行比较。
- 温故而知新-----带有命名方法的委托和带有匿名方法的委托
- 匿名方法在委托实例化中的应用
- 带有命名方法的委托与带有匿名方法的委托(C# 编程指南)
- 转帖:用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树
- 定义一个交通工具(Vehicle)的类,在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变
- 用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树
- C# 委托用法、匿名方法、Lambda表达式
- 委托的匿名方法,以及在协变与逆变中的应用
- 基础代码分析之四:委托与匿名方法
- 从.NET中委托写法的演变谈开去(上):委托与匿名方法(转)
- 使用匿名方法 委托
- C#基础:委托、匿名方法、事件
- 委托,匿名方法,Lambda,泛型委托,表达式树