[关闭]
@laofang 2016-07-10T04:11:34.000000Z 字数 2530 阅读 1042

委托

.Net


委托

简介

概念: 为了减少重复代码或者增强代码灵活性(下面的两个比较方法可以看出来)可以将方法作为函数传递给一个方法. 用一种数据类型来表示这种可以作为参数的方法, 这种数据叫做委托.

声明: 使用deletage关键字, 后面跟着想是方法声明的东西, 这个方法的签名是委托所引用的方法的签名, 正常方法声明中的方法名称的位置要替换成委托类型的名称.

  1. public delegate bool ComparisonHandler(int first, int second

不使用委托的冒泡排序

  1. using System;
  2. namespace Console_Test
  3. {
  4. public class Program {
  5. public enum sortType
  6. {
  7. Greterthan,
  8. Lessthan
  9. }
  10. public static void BubbleSort(int []items, sortType sortType)
  11. {
  12. if (items == null) return;
  13. for(int i=items.Length-1; i>=0; i--)
  14. {
  15. for(int j=1; j<= i; j++)
  16. {
  17. bool swap = false;
  18. switch (sortType)//比较方法, 代码僵硬不易扩展
  19. {
  20. case sortType.Greterthan:
  21. swap = items[j - 1] > items[j];break;
  22. case sortType.Lessthan:
  23. swap = items[j - 1] < items[j]; break;
  24. }
  25. if (swap)
  26. {
  27. int temp = items[j - 1];
  28. items[j - 1] = items[j];
  29. items[j] = temp;
  30. }
  31. }
  32. }
  33. }
  34. static void Main(string[] args)
  35. {
  36. int[] items = { 1, 2, 4, 3, 3, 13, 34, 1, 45, 9 };
  37. BubbleSort(items, sortType.Greterthan);
  38. for(int i=0; i<items.Length; i++)
  39. {
  40. Console.Write(items[i]+" ");
  41. }
  42. Console.WriteLine();
  43. BubbleSort(items, sortType.Lessthan);
  44. for (int i = 0; i < items.Length; i++)
  45. {
  46. Console.Write(items[i] + " ");
  47. }
  48. }
  49. }
  50. }

使用委托的冒泡排序

  1. using System;
  2. namespace Console_Test
  3. {
  4. //声明委托
  5. public delegate bool ComparisonHander(int first, int second);
  6. class Program
  7. {
  8. //声明一个方法(匹配委托签名)
  9. //升序
  10. public static bool Graterthan(int first, int second)
  11. {
  12. return first > second;
  13. }
  14. //降序
  15. public static bool Lessthan(int first, int second)
  16. {
  17. return first < second;
  18. }
  19. //冒泡排序
  20. public static void BubbleSort(int[] items, ComparisonHander comparisonMethod)
  21. {
  22. int i;
  23. int j;
  24. int temp;
  25. if (comparisonMethod == null)
  26. {
  27. throw new ArgumentNullException("comparisonMethod");
  28. }
  29. if (items == null)
  30. {
  31. return;
  32. }
  33. for (i = items.Length - 1; i >= 0; i--)
  34. {
  35. for (j = 1; j <= i; j++)
  36. {
  37. if (comparisonMethod(items[j - 1], items[j]))
  38. {
  39. temp = items[j - 1];
  40. items[j - 1] = items[j];
  41. items[j] = temp;
  42. }
  43. }
  44. }
  45. }
  46. static void Main(string[] args)
  47. {
  48. int[] items = { 1, 2, 4, 3, 3, 13, 34, 1, 45, 9 };
  49. //使用委托
  50. BubbleSort(items, Graterthan);
  51. for(int i=0; i<items.Length; i++)
  52. {
  53. Console.Write(items[i]+" ");
  54. }
  55. Console.WriteLine();
  56. BubbleSort(items, Lessthan);
  57. for (int i = 0; i < items.Length; i++)
  58. {
  59. Console.Write(items[i] + " ");
  60. }
  61. }
  62. }
  63. }

Lambda表达式

简介

概念: 使用精简的方法创建委托
分类: 语句Lambda & 表达式Lambda

语句Lambda

结构: (形参列表) => (代码块)

  1. BubbleSort(items,(int first,int second)=>{return first < second>
  2. //参数类型可省略: BubbleSort(items,(first,second)=>{return first < second>
  3. //单个参数Lambda
  4. var x = data.Where( i => {return i>0;});
  5. //无参Lambda
  6. Func<string> getUserInput =
  7. () =>{
  8. string input;
  9. do{
  10. input = Console.ReadLine();
  11. }
  12. while(intput.Trim().Length != 0);
  13. return input;
  14. }

表达式Lambda

结构: 参数 => 表达式

  1. BubbleSort(items, (first,second) => first < second);

注意事项:
Lambda表达式没有类型

  • 不能访问成员
  • 不能出现在is操作的左侧
  • 不能用于推断局部变量类型
  • 不能在Lambda使用跳转

通用委托 : System.Func 和 System.Action

System.Func 代表有返回值的方法
Systen.Action 代表返回值为void的方法

  1. //声明排序方法
  2. void BubbleSort(int[] items, Func<int, int , bool) compartsonMethod){...}
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注