委托类型 (delegate type) 表示对具有特定参数列表和返回类型的方法的引用。通过委托,我们能够将方法作为实体赋值给变量和作为参数传递。委托类似于在其他某些语言中的函数指针的概念,但是与函数指针不同,委托是面向对象的,并且是类型安全的。

    将一个方法作为参数传递给另一个方法。
    委托所指向的函数必须跟委托具有相同的签名。(参数和返回值类型 )

    1. using System;
    2. namespace _143_委托引入
    3. {
    4. public delegate void DelSayHi(string name);
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. DelSayHi del = SayHiChinese; // new DelSayHi(SayHiChinese);
    10. del("微咲");
    11. Test("wexiao", SayHiEnglish);
    12. //匿名函数
    13. DelSayHi ndel =
    14. delegate (string name)
    15. {
    16. Console.WriteLine("我是匿名函数" + name);
    17. };
    18. ndel("hello one");
    19. //lambda表达式 => goes to ;;;;;;;;;;
    20. DelSayHi ndel2 = (string name) => { Console.WriteLine(name + "lambda表达式"); };
    21. ndel2("lambda");
    22. Console.ReadKey();
    23. }
    24. public static void Test(string name, DelSayHi del)
    25. {
    26. //调用
    27. del(name);
    28. }
    29. public static void SayHiChinese(string name)
    30. {
    31. Console.WriteLine("你好" + name);
    32. }
    33. public static void SayHiEnglish(string name)
    34. {
    35. Console.WriteLine("Hello" + name);
    36. }
    37. }
    38. }
    1. using System;
    2. namespace _142_为什么使用委托
    3. {
    4. public delegate string DelProStr(string name);
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. string[] names = { "aBcDEFg", "HijKLmNOPq", "RxYZ" };
    10. //ProStrToUpper(names);
    11. //roStrSYH(names);
    12. ProStr(names, ProStrToLower);
    13. //匿名函数
    14. ProStr(names, delegate (string name)
    15. {
    16. return name.ToUpper();
    17. }
    18. );
    19. for (int i = 0; i < names.Length; i++)
    20. {
    21. Console.WriteLine(names[i]);
    22. }
    23. Console.ReadKey();
    24. }
    25. //将一个方法作为参数传入方法==委托
    26. public static void ProStr(string[] names,DelProStr del)
    27. {
    28. for (int i = 0; i < names.Length; i++)
    29. {
    30. names[i] = del(names[i]);
    31. }
    32. }
    33. public static string ProStrToUpper(string name)
    34. {
    35. return name.ToUpper();
    36. }
    37. public static string ProStrToLower(string name)
    38. {
    39. return name.ToLower();
    40. }
    41. public static string ProStrSYH(string name)
    42. {
    43. return "\"" + name + "\"";
    44. }
    45. //public static void ProStrToUpper(string[] names)
    46. //{
    47. // for (int i = 0; i < names.Length; i++)
    48. // {
    49. // names[i] = names[i].ToUpper();
    50. // }
    51. //}
    52. //public static void ProStrToLower(string[] names)
    53. //{
    54. // for (int i = 0; i < names.Length; i++)
    55. // {
    56. // names[i] = names[i].ToLower();
    57. // }
    58. //}
    59. //public static void ProStrSYH(string[] names)
    60. //{
    61. // for (int i = 0; i < names.Length; i++)
    62. // {
    63. // names[i] = "\"" + names[i] + "\"";
    64. // }
    65. //}
    66. }
    67. }

    委托练习:
    求任意数组的最大值

    1. using System;
    2. namespace _144_使用委托求任意数组最大值
    3. {
    4. //声明一个委托:比较o1与o2大小
    5. public delegate int DelCompare(object o1, object o2);
    6. class Program
    7. {
    8. static void Main(string[] args)
    9. {
    10. object[] o = { 1, 2, 3, 4, 5 };
    11. object result = GetMax(o, CompareInt);
    12. Console.WriteLine(result);
    13. object[] o1 = { "dwadawd", "dwaudhwauf", "我是你亲爱的老父亲咿呀咿呀哟咿呀咿呀哟", "wduhwauifgauibgysahjdn" };
    14. result = GetMax(o1, CompareString);
    15. Console.WriteLine(result);
    16. Console.ReadKey();
    17. }
    18. /// <summary>
    19. /// 求数组最大值
    20. /// </summary>
    21. /// <param name="nums">要求最大值的数组</param>
    22. /// <param name="del">比较最大值和数组元素的委托</param>
    23. /// <returns>最大值</returns>
    24. public static object GetMax(object[] nums, DelCompare del)
    25. {
    26. object max = nums[0];
    27. for (int i = 0; i < nums.Length; i++)
    28. {
    29. //要传一个比较的方法, 如果max<nums[i]就会执行if
    30. if (del(max, nums[i]) < 0)
    31. {
    32. max = nums[i];
    33. }
    34. }
    35. return max;
    36. }
    37. /// <summary>
    38. /// 比较int型数据的大小
    39. /// </summary>
    40. /// <param name="o1"></param>
    41. /// <param name="o2"></param>
    42. /// <returns></returns>
    43. public static int CompareInt(object o1, object o2)
    44. {
    45. int n1 = (int)o1;
    46. int n2 = (int)o2;
    47. return n1 - n2;
    48. }
    49. /// <summary>
    50. /// 比较string型数据的大小
    51. /// </summary>
    52. /// <param name="o1"></param>
    53. /// <param name="o2"></param>
    54. /// <returns></returns>
    55. public static int CompareString(object o1, object o2)
    56. {
    57. string s1 = (string)o1;
    58. string s2 = (string)o2;
    59. return s1.Length - s2.Length;
    60. }
    61. }
    62. }

    使用泛型委托求任意数组的最大值

    1. using System;
    2. namespace _145_泛型委托求数组最大值
    3. {
    4. //声明一个委托:比较o1与o2大小
    5. public delegate int DelCompare<T>(T t1,T t2);
    6. //public delegate int DelCompare(object o1, object o2);
    7. class Program
    8. {
    9. static void Main(string[] args)
    10. {
    11. int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
    12. int max= GetMax<int>(nums, CompareInt);
    13. Console.WriteLine(max);
    14. string[] names = { "微咲", "我是你爹", "sduahwuifhwauibfwabndhwabdywaubfy" };
    15. string maxLenth = GetMax<string>(names, CompareString);
    16. Console.WriteLine(maxLenth);
    17. Console.ReadKey();
    18. }
    19. /// <summary>
    20. /// 求数组最大值
    21. /// </summary>
    22. /// <param name="nums">要求最大值的数组</param>
    23. /// <param name="del">比较最大值和数组元素的委托</param>
    24. /// <returns>最大值</returns>
    25. public static T GetMax<T>(T[] nums, DelCompare<T> del)
    26. {
    27. T max = nums[0];
    28. for (int i = 0; i < nums.Length; i++)
    29. {
    30. //要传一个比较的方法, 如果max<nums[i]就会执行if
    31. if (del(max, nums[i]) < 0)
    32. {
    33. max = nums[i];
    34. }
    35. }
    36. return max;
    37. }
    38. /// <summary>
    39. /// 比较int型数据的大小
    40. /// </summary>
    41. /// <param name="n1"></param>
    42. /// <param name="n2"></param>
    43. /// <returns></returns>
    44. public static int CompareInt(int n1, int n2)
    45. {
    46. return n1 - n2;
    47. }
    48. /// <summary>
    49. /// 比较string型数据的大小
    50. /// </summary>
    51. /// <param name="s1"></param>
    52. /// <param name="s2"></param>
    53. /// <returns></returns>
    54. public static int CompareString(string s1, string s2)
    55. {
    56. return s1.Length - s2.Length;
    57. }
    58. }
    59. }