C#内置泛型委托:Action委托
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委托的更多相关文章
- c# 匿名方法(函数) 匿名委托 内置泛型委托 lamada
匿名方法:通过匿名委托 .lamada表达式定义的函数具体操作并复制给委托类型: 匿名委托:委托的一种简单化声明方式通过delegate关键字声明: 内置泛型委托:系统已经内置的委托类型主要是不带返回 ...
- C#内置泛型委托:Func委托
1.什么是Func委托 Func委托代表有返回类型的委托 2.Func委托定义 查看Func的定义: using System.Runtime.CompilerServices; namespace ...
- C# 匿名方法 委托 Action委托 Delegate委托
原文地址:https://msdn.microsoft.com/zh-cn/library/bb882516.aspx 匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用. 可以使用匿 ...
- .NET (三)委托第三讲:内置委托Action
.NET 为我们提供了无返回值的内置委托 Action,代码如下: // 摘要: // 封装一个方法,该方法只有一个参数并且不返回值. // // 参数: // obj: // 此委托封装的方法的参数 ...
- C#内置委托类型Func和Action对比及用法
C#的内置委托类型 Func Action 返回值 有(只有一个Tresult) 无 重载 17个(0参-16参) 17个(0参-16参) 泛型 支持 支持 系统内置 是 是 是否需要声明 否 否 c ...
- CLR环境中内置了几个常用委托(转)
CLR环境中给我们内置了几个常用委托Action. Action<T>.Func<T>.Predicate<T>,一般我们要用到委托的时候,尽量不要自己再定义一 个 ...
- C#语法糖之第五篇: 泛型委托- Action<T>
因为工作的原因(其实还是个人的惰性)昨天没有给大家分享文章,然后这几天也有很多园友也提出了他们报告的意见及指导,再次感谢这些兄弟们的照顾我 和支持,这个分类的文章我当时想的是把我的学习经验和工作中用到 ...
- C#常用的内置委托
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- asp。net内置委托
Action与Func是APS.NET内置委托 //--------------无返回值的委托Action--------------------------- Action是无返回值的泛型委托 Ac ...
随机推荐
- 如何使用Git 下载GitHub的东西
1. 先安装git 2. 注册一个github账号 3. 新建一个项目 3. 打开git 运行以下命令: cd /d/workspace (切换目录到 d盘的workspace) git init ( ...
- php的opcode缓存
前言:由php的运行机制决定,其实php在运行阶段我们也是可以进行缓存的从而提高程序运行效率,这就是我们常说的opcode缓存.1.简述php的运行机制(因为本文是写opcode缓存的所以这里只是简要 ...
- tcp拥堵算法
http://m.blog.csdn.net/article/details?id=6739189
- 挂载ios,error tip:mount: wrong fs type, bad option, bad superblock on /dev/loop0,
挂载ios,tip: mount -t iso9660 -o loop 111.iso /isofiles 有可能是-t参数有问题,把-t参数去掉,然后挂载,就成功了
- SVN四部曲之SVN设置详解深入
想知道不同的设置是干什么用的,你只需将鼠标指针在编辑框/选项框上停留一秒钟...一个帮助提示气泡就会弹出来. 常规设置 图 4.68. 设置对话框,常规设置页面 这个对话框允许你指定自己喜欢的语言,同 ...
- c#去除字符串中的空格,回车,换行符,制表符
string l_strResult = str.Replace("\n", "").Replace(" ","").R ...
- 查看chekpoit文件
使用tf.train.Saver()保存到checkpoint文件,我们可以用tensorflow查看. # import the inspect_checkpoint library from te ...
- Oracle用户密码过期的处理方法
受影响版本:Oracle11g以上版本. 导致密码消失的原因:Oracle 11g中默认的DEFAULT概要文件中口令有效期PASSWORD_LIFE_TIME默认值为180天. 当以客户端登 ...
- JAVA-JSP内置对象之session范围
相关资料:<21天学通Java Web开发> session范围1.就是指客户浏览器与服务器一次会话范围内,如果和服务器断开连接,那么这个属性也就失效了.2.通过使用session的set ...
- ARKit从入门到精通(1)-ARKit初体验
ARKit从入门到精通(1)-ARKit初体验 转载自:http://blog.csdn.net/u013263917/article/details/72903174 该系列文章共十篇,笔者将由易到 ...