广告位联系
返回顶部
分享到

C#内置泛型委托之Action委托的详解

C#教程 来源:互联网 作者:秩名 发布时间:2022-03-14 09:25:33 人浏览
摘要

1、什么是Action泛型委托 ActionT是.NET Framework内置的泛型委托,可以使用ActionT委托以参数形式传递方法,而不用显示声明自定义的委托。封装的方法必须与此委托定义的方法签名相对应。

1、什么是Action泛型委托

Action<T>是.NET Framework内置的泛型委托,可以使用Action<T>委托以参数形式传递方法,而不用显示声明自定义的委托。封装的方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能有返回值。

2、Action委托定义

查看Action的定义:

1

2

3

4

5

6

7

8

9

10

using System.Runtime.CompilerServices;

 

namespace System

{

    //

    // 摘要:

    //     封装一个方法,该方法不具有参数且不返回值。

    [TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")]

    public delegate void Action();

}

你会发现,Action其实就是没有返回值的delegate。

3、示例

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

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

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

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

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

代码示例如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ActionDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            // 无参数无返回值的委托

            Action action1 = new Action(ActionWithNoParaNoReturn);

            action1();

            Console.WriteLine("----------------------------");

            // 使用delegate

            Action action2 = delegate { Console.WriteLine("这里是使用delegate"); };

            // 执行

            action2();

            Console.WriteLine("----------------------------");

            // 使用匿名委托

            Action action3 = () => { Console.WriteLine("这里是匿名委托"); };

            action3();

            Console.WriteLine("----------------------------");

            // 有参数无返回值的委托

            Action<int> action4 = new Action<int>(ActionWithPara);

            action4(23);

            Console.WriteLine("----------------------------");

            // 使用delegate

            Action<int> action5 = delegate (int i) { Console.WriteLine($"这里是使用delegate的委托,参数值是:{i}"); };

            action5(45);

            Console.WriteLine("----------------------------");

            // 使用匿名委托

            Action<string> action6 = (string s) => { Console.WriteLine($"这里是使用匿名委托,参数值是:{s}"); };

            action6("345");

            Console.WriteLine("----------------------------");

            // 多个参数无返回值的委托

            Action<int, string> action7 = new Action<int, string>(ActionWithMulitPara);

            action7(7, "abc");

            Console.WriteLine("----------------------------");

            // 使用delegate

            Action<int, int, string> action8 = delegate (int i1, int i2, string s)

            {

                Console.WriteLine($"这里是三个参数的Action委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s}");

            };

            action8(12, 34, "abc");

            Console.WriteLine("----------------------------");

            Action<int,int,string, string> action9 = (int i1,int i2, string s1,string s2) =>

            {

                Console.WriteLine($"这里是使用四个参数的委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s1},参数4的值是:{s2}");

            };

            // 执行委托

            action9(34,56, "abc","def");

            Console.ReadKey();

        }

 

 

 

 

        static void ActionWithNoParaNoReturn()

        {

            Console.WriteLine("这是无参数无返回值的Action委托");

        }

 

        static void ActionWithPara(int i)

        {

            Console.WriteLine($"这里是有参数无返回值的委托,参数值是:{i}");

        }

 

        static void ActionWithMulitPara(int i,string s)

        {

            Console.WriteLine($"这里是有两个参数无返回值的委托,参数1的值是:{i},参数2的值是:{s}");

        }

    }

}

运行结果:

4、真实示例

先看下面一张截图:

从截图中可以看出:ForEach()方法的参数是一个参数类型是T的无返回值的Action委托,下面的示例中利用Action委托作为参数传递给ForEach()方法。

1、定义Student实体类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ActionDemo

{

    public class Student

    {

        public int Id { get; set; }

 

        public string Name { get; set; }

 

        public int Age { get; set; }

 

        public int Sex { get; set; }

    }

}

2、利用ForEach()方法输出集合内容

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ActionDemo

{

    public class ActionTest

    {

        public static void Test()

        {

            List<Student> list = new List<Student>()

            {

              new Student(){Id=1,Name="张三",Age=19,Sex=1},

              new Student(){Id=2,Name="李四",Age=20,Sex=2},

              new Student(){Id=3,Name="王五",Age=23,Sex=1},

              new Student(){Id=4,Name="赵六",Age=18,Sex=1}

            };

 

            // Action<Student>委托作为参数传递给ForEach()方法

            list.ForEach(student =>

            {

                Console.WriteLine($"姓名:{student.Name},年龄:{student.Age}");

            });

        }

    }

}

3、在Main()方法中调用

1

ActionTest.Test();

4、结果


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://www.cnblogs.com/dotnet261010/p/10108791.html
相关文章
  • WPF实现窗体亚克力效果的代码

    WPF实现窗体亚克力效果的代码
    WPF 窗体设置亚克力效果 框架使用大于等于.NET40。 Visual Studio 2022。 项目使用MIT开源许可协议。 WindowAcrylicBlur设置亚克力颜色。 Opacity设置透
  • C#非托管泄漏中HEAP_ENTRY的Size对不上解析

    C#非托管泄漏中HEAP_ENTRY的Size对不上解析
    一:背景 1. 讲故事 前段时间有位朋友在分析他的非托管泄漏时,发现NT堆的_HEAP_ENTRY的 Size 和!heap命令中的 Size 对不上,来咨询是怎么回事?
  • C#中ArrayList 类的使用介绍
    一:ArrayList 类简单说明 动态数组ArrayList类在System.Collecions的命名空间下,所以使用时要加入System.Collecions命名空间,而且ArrayList提供添加,
  • C#使用BinaryFormatter类、ISerializable接口、XmlSeriali

    C#使用BinaryFormatter类、ISerializable接口、XmlSeriali
    序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程。对象一旦被序列化,就可以把对象状态保存到硬盘的某个位
  • C#序列化与反序列化集合对象并进行版本控制
    当涉及到跨进程甚至是跨域传输数据的时候,我们需要把对象序列化和反序列化。 首先可以使用Serializable特性。 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  • C#事件中关于sender的用法解读

    C#事件中关于sender的用法解读
    C#事件sender的小用法 开WPF新坑了,看了WPF的炫酷界面,再看看winForm实在是有些惨不忍睹(逃)。后面会开始写一些短的学习笔记。 一、什么
  • 在C#程序中注入恶意DLL的方法

    在C#程序中注入恶意DLL的方法
    一、背景 前段时间在训练营上课的时候就有朋友提到一个问题,为什么 Windbg 附加到 C# 程序后,程序就处于中断状态了?它到底是如何实现
  • 基于C#实现一个简单的FTP操作工具
    实现功能 实现使用FTP上传、下载、重命名、刷新、删除功能 开发环境 开发工具: Visual Studio 2013 .NET Framework版本:4.5 实现代码 1 2 3 4 5 6 7
  • C#仿QQ实现简单的截图功能

    C#仿QQ实现简单的截图功能
    接上一篇写的截取电脑屏幕,我们在原来的基础上加一个选择区域的功能,实现自定义选择截图。 个人比较懒,上一篇的代码就不重新设计
  • C#实现线性查找算法的介绍
    线性查找,肯定是以线性的方式,在集合或数组中查找某个元素。 通过代码来理解线性查找 什么叫线性?还是在代码中体会吧。 首先需要一
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计