1、什么是Action泛型委托

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

2、Action委托定义

查看Action的定义:

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型参数,无返回值的委托。

代码示例如下:

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();
Console.WriteLine("----------------------------");
// 使用delegate
Action<int> action5 = delegate (int i) { Console.WriteLine($"这里是使用delegate的委托,参数值是:{i}"); };
action5();
Console.WriteLine("----------------------------");
// 使用匿名委托
Action<string> action6 = (string s) => { Console.WriteLine($"这里是使用匿名委托,参数值是:{s}"); };
action6("");
Console.WriteLine("----------------------------");
// 多个参数无返回值的委托
Action<int, string> action7 = new Action<int, string>(ActionWithMulitPara);
action7(, "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(, , "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(,, "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实体类

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()方法输出集合内容

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=,Name="张三",Age=,Sex=},
new Student(){Id=,Name="李四",Age=,Sex=},
new Student(){Id=,Name="王五",Age=,Sex=},
new Student(){Id=,Name="赵六",Age=,Sex=}
}; // Action<Student>委托作为参数传递给ForEach()方法
list.ForEach(student =>
{
Console.WriteLine($"姓名:{student.Name},年龄:{student.Age}");
});
}
}
}

3、在Main()方法中调用

ActionTest.Test();

4、结果

C#内置泛型委托:Action委托的更多相关文章

  1. c# 匿名方法(函数) 匿名委托 内置泛型委托 lamada

    匿名方法:通过匿名委托 .lamada表达式定义的函数具体操作并复制给委托类型: 匿名委托:委托的一种简单化声明方式通过delegate关键字声明: 内置泛型委托:系统已经内置的委托类型主要是不带返回 ...

  2. C#内置泛型委托:Func委托

    1.什么是Func委托 Func委托代表有返回类型的委托 2.Func委托定义 查看Func的定义: using System.Runtime.CompilerServices; namespace ...

  3. C# 匿名方法 委托 Action委托 Delegate委托

    原文地址:https://msdn.microsoft.com/zh-cn/library/bb882516.aspx 匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿 ...

  4. .NET (三)委托第三讲:内置委托Action

    .NET 为我们提供了无返回值的内置委托 Action,代码如下: // 摘要: // 封装一个方法,该方法只有一个参数并且不返回值. // // 参数: // obj: // 此委托封装的方法的参数 ...

  5. C#内置委托类型Func和Action对比及用法

    C#的内置委托类型 Func Action 返回值 有(只有一个Tresult) 无 重载 17个(0参-16参) 17个(0参-16参) 泛型 支持 支持 系统内置 是 是 是否需要声明 否 否 c ...

  6. CLR环境中内置了几个常用委托(转)

    CLR环境中给我们内置了几个常用委托Action. Action<T>.Func<T>.Predicate<T>,一般我们要用到委托的时候,尽量不要自己再定义一 个 ...

  7. C#语法糖之第五篇: 泛型委托- Action<T>

    因为工作的原因(其实还是个人的惰性)昨天没有给大家分享文章,然后这几天也有很多园友也提出了他们报告的意见及指导,再次感谢这些兄弟们的照顾我 和支持,这个分类的文章我当时想的是把我的学习经验和工作中用到 ...

  8. C#常用的内置委托

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. asp。net内置委托

    Action与Func是APS.NET内置委托 //--------------无返回值的委托Action--------------------------- Action是无返回值的泛型委托 Ac ...

随机推荐

  1. 内心的平静就是财富本身-Cell组件-用友华表的由来-T君

    时至今日,Cell组件仍是应用广泛的商业报表组件 作者:人生三毒 编者注:本文作者人生三毒为知名网站及网页游戏公司创始人,此前曾为IT类媒体资深编辑,见证了中国互联网早期的发展. 认识T君之前先认识的 ...

  2. GitHub创始人:我如何放弃30万美元年薪创业

    GitHub创始人:我如何放弃30万美元年薪创业 本文摘自GitHub创始人Tom Preston Werner个人博客. 时间还在2007年,我一个人独坐旧金山的Zeke 体育酒吧内.其实我并不经常 ...

  3. 前端表单中有按钮button自动提交表单

    问题描述 在设计表单时,表单内有一个按钮<button>,该按钮是用来获取其他数据或执行其他操作的.并不是让他提交表单. 解决方案 1) 设置 form 的 onsubmit='retur ...

  4. git 修改历史提交信息

    当你不小心,写错了提交的注视/信息,该如何处理呢.理论上,SCM是不应该修改历史的信息的,提交的注释也是.   不过在git中,其commit提供了一个--amend参数,可以修改最后一次提交的信息. ...

  5. 【Android开发】Android应用程序目录结构

    原文:http://android.eoe.cn/topic/summary Android开发之旅:组件生命周期吴秦 Android开发之旅:HelloWorld项目的目录结构 * HelloWor ...

  6. [svc]Linux vmstat命令实战详解

    vmstat输出 注:是cpu 内存 磁盘 虚拟内存交换情况 io读写情况 vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存 ...

  7. Linux 索引节点(inode)详解

    参考文章:http://www.ruanyifeng.com/blog/2011/12/inode.html

  8. 常用的NodeJS模块

    图片处理 1.Manipulate images 官网:http://github.com/aheckmann/gm ImageMagick和GraphicsMagick主要用于图片的创建.编辑.合成 ...

  9. express中的路径区别

    请求的url:http://localhost:3000/api/article/upload?q=1000&n=tom请求方法:postconsole.log('hostname==='+r ...

  10. Android-一只手指滑动View,另一只手指按Home键,重新进入后View状态无法更新的问题

    上午测试报了一个bug:说是一只手指拖动虚拟摇杆上的View滑块不松,另一只手指点击Home键将App压后台,重新进入的时候,View滑块卡死了. 刚开始看到这个问题感觉很奇怪,因为正常情况下,手指离 ...