• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

《C#本质论》读书笔记(12)委托和Lambda表达式

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

12.1.委托概述

12.1.2 委托的数据类型

为了减少重复代码数量,可以将比较方法作为参数传递给 BubbleSort()方法。此外,为了将方法作为参数传递,必须有一个能够标识方法的数据类型——也就是委托。这里的委托类型是 ComparisonHandler 。
 c# 2.0之前的写法
  1. class DelegateSample  

  2.     {  

  3.         static void Main(string[] args)  

  4.         {  

  5.             //int[] arr = { 10, 20, 30, 40, 50 };  

  6.             int[] arr = { 50, 40, 30, 20, 10 };  

  7.   

  8.             ConsoleArr(arr);  

  9.   

  10.             ComparisonHandler wx = new ComparisonHandler(DelegateSample.IsTrue);  

  11.             BubbleSort(arr, wx);  

  12.       //C#2.0之前是这么写的


  13.       //BubbleSort(arr, new ComparisonHandler(IsTrue));  
  14.   

  15.             ConsoleArr(arr);  

  16.   

  17.             Console.Read();  

  18.         }  

  19.   

  20.         public delegate bool ComparisonHandler(int a, int b);  

  21.   

  22.         public static bool IsTrue(int a, int b)  

  23.         {  

  24.                 return a > b;  

  25.         }  

  26.   

  27.         public static void BubbleSort(int[] items, ComparisonHandler comparisonMethod)  

  28.         {  

  29.             int i;  

  30.             int j;  

  31.             int temp;  

  32.             if (items == null)  

  33.             {  

  34.                 return;  

  35.             }  

  36.             if (comparisonMethod == null)  

  37.             {  

  38.                 throw new ArgumentNullException("comparisonMethod");  

  39.             }  

  40.             for (i = items.Length - 1; i >= 0; i--)  

  41.             {  

  42.                 for (j = 1; j <= i; j++)  

  43.                 {  

  44.                     if (comparisonMethod(items[j - 1], items[j]))  

  45.                     {  

  46.                         temp = items[j - 1];  

  47.                         items[j - 1] = items[j];  

  48.                         items[j] = temp;  

  49.                     }  

  50.                 }  

  51.             }  

  52.         }  

  53.   

  54.         public static void ConsoleArr(int[] arr)  

  55.         {  

  56.             foreach (var item in arr)  

  57.             {  

  58.                 Console.Write(item+",");  

  59.             }  

  60.             Console.WriteLine();  

  61.         }  

  62.     }  



C#2.0以后可以直接调用方法
 
  1. public static bool AlphabeticalIsTrue(int a,int b)  

  2.         {  

  3.             int comparison;  

  4.             comparison = (a.ToString().CompareTo(b.ToString()));  

  5.             return comparison > 0;  

  6.         }  

  7.   

  8.   

  9. //C# 2.0以后直接传递方法  

  10.             BubbleSort(arr, AlphabeticalIsTrue); 


12.1.3 委托内部机制

第一个属性属于 System.Reflection.MethodiInfo 类型,MethodInfo 定义一个特定方法的签名,其中包括方法的名称、参数和返回类型。除了 MethodInfo,委托还需要一个对象实例,其中包含了要调用的方法。这正式第二个属性 Target 的用途。在静态方法的情况下,Target 对应于类型自身。

  1. // 摘要:   

  2. //     初始化一个委托,该委托对指定的类实例调用指定的实例方法。  

  3. //  

  4. // 参数:   

  5. //   target:  

  6. //     类实例,委托对其调用 method。  

  7. //  

  8. //   method:  

  9. //     委托表示的实例方法的名称。  

  10. //  

  11. // 异常:   

  12. //   System.ArgumentNullException:  

  13. //     target 为 null。 - 或 - method 为 null。  

  14. //  

  15. //   System.ArgumentException:  

  16. //     绑定到目标方法时出错。  

  17. [SecuritySafeCritical]  

  18. protected Delegate(object target, string method);  

  19. //  

  20. // 摘要:   

  21. //     初始化一个委托,该委托从指定的类调用指定的静态方法  

  22. //  

  23. // 参数:   

  24. //   target:  

  25. //     System.Type,它表示定义 method 的类。  

  26. //  

  27. //   method:  

  28. //     委托表示的静态方法的名称。  

  29. //  

  30. // 异常:   

  31. //   System.ArgumentNullException:  

  32. //     target 为 null。 - 或 - method 为 null。  

  33. //  

  34. //   System.ArgumentException:  

  35. //     target 不是 RuntimeType。 请参见 反射中的运行时类型。 - 或 - target 表示开放式泛型类型。  

  36. [SecuritySafeCritical]  

  37. protected Delegate(Type target, string method);  



12.2.匿名方法



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#使用Redis的基本操作发布时间:2022-07-10
下一篇:
c#遍历Mysql所有表所有列,查找目标数据发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap