• .NET Framework中自带的泛型委托Action


    Action<>是.NET Framework中自带的泛型委托,可以接收一个或多个输入参数,但不返回任何参数,可传递至多16种不同类型的参数类型。在Linq的一些方法上使用的比较多。

     

    1、Action泛型委托

    .NET Framework为我们提供了多达16个参数的Action委托定义,对于常见的开发场景已经完全够用。

    如下图,

    示例说明:

    Action<>:委托至少0个参数,至多16个参数,无返回值。

    Action:表示无参,无返回值的委托。

    Action :表示有传入参数int,string无返回值的委托。

    Action:表示有传入参数int,string,bool无返回值的委托。

    Action:表示有传入4个int型参数,无返回值的委托。

    2、Action泛型委托的使用

    Action泛型委托的使用方法,可以通过下面代码看一下,

    例如,

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace ActionDemo
    7. {
    8. class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. // 无参数无返回值的委托
    13. Action action1 = new Action(ActionWithNoParaNoReturn);
    14. action1();
    15. Console.WriteLine("----------------------------");
    16. // 使用delegate
    17. Action action2 = delegate { Console.WriteLine("使用delegate"); };
    18. // 执行
    19. action2();
    20. Console.WriteLine("----------------------------");
    21. // 使用匿名委托
    22. Action action3 = () => { Console.WriteLine("匿名委托"); };
    23. action3();
    24. Console.WriteLine("----------------------------");
    25. // 有参数无返回值的委托
    26. Action<int> action4 = new Action<int>(ActionWithPara);
    27. action4(11);
    28. Console.WriteLine("----------------------------");
    29. // 使用delegate
    30. Action<int> action5 = delegate (int i) { Console.WriteLine($"使用delegate的委托,参数值是:{i}"); };
    31. action5(22);
    32. Console.WriteLine("----------------------------");
    33. // 使用匿名委托
    34. Action<string> action6 = (string s) => { Console.WriteLine($"使用匿名委托,参数值是:{s}"); };
    35. action6("C#");
    36. Console.WriteLine("----------------------------");
    37. // 多个参数无返回值的委托
    38. Action<int,string> action7 = new Action<int,string>(ActionWithMulitPara);
    39. action7(33, "Java");
    40. Console.WriteLine("----------------------------");
    41. // 使用delegate
    42. Action<int,int,string> action8 = delegate (int i1, int i2, string s)
    43. {
    44. Console.WriteLine($"三个参数的Action委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s}");
    45. };
    46. action8(44, 55, "Python");
    47. Console.WriteLine("----------------------------");
    48. Action<int,int,string,string> action9 = (int i1,int i2, string s1,string s2) =>
    49. {
    50. Console.WriteLine($"使用四个参数的委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s1},参数4的值是:{s2}");
    51. };
    52. // 执行委托
    53. action9(66,77, "C","CJavaPy");
    54. Console.ReadKey();
    55. }
    56. static void ActionWithNoParaNoReturn()
    57. {
    58. Console.WriteLine("无参数无返回值的Action委托");
    59. }
    60. static void ActionWithPara(int i)
    61. {
    62. Console.WriteLine($"有参数无返回值的委托,参数值是:{i}");
    63. }
    64. static void ActionWithMulitPara(int i,string s)
    65. {
    66. Console.WriteLine($"有两个参数无返回值的委托,参数1的值是:{i},参数2的值是:{s}");
    67. }
    68. }
    69. }

     

  • 相关阅读:
    Linux宝塔面板高并发优化方案
    高仿英雄联盟游戏网页制作作业 英雄联盟LOL游戏HTML网页设计模板 简单学生网页设计 静态HTML CSS网站制作成品
    万能适配器basequickadapter + recycleview实现单选并且默认选择第一个
    大唐“痴情”男女-白居易-湘灵
    PHP:构造函数和析构函数
    Linux 基础操作手记四
    ORA-22992 cannot use LOB locators selected from remote tables
    【pytorch】tensorboard + transforms的使用
    Java多线程(7)----浅谈线程池
    艾美捷nickases-Cas9内切酶裂解试验展示
  • 原文地址:https://blog.csdn.net/lwf3115841/article/details/134256850