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 ...
随机推荐
- Mongodb是用Sum 和Where条件
Sum 按照条件求和 db.aa.aggregate([ { $group: { _id: null, total: { $sum: "$value" } } }, //$valu ...
- Atitit 项目源码的架构,框架,配置与环境说明模板 规范 标准化
Atitit 项目源码的架构,框架,配置与环境说明模板 规范 标准化 版本1.0 作者 艾龙 attilax 1. 概述:核心业务: 1 1.1. 功能文档路径 /palmWin/src/docum ...
- Vivado下生成及烧写MCS文件
Jtag模式: 1.打开Open Hardware Manager 2. Tools ->Auto Connect 3.TCL输入: write_cfgmem -format MCS -size ...
- 【Unity】第7章 输入控制
分类:Unity.C#.VS2015 创建日期:2016-04-21 一.简介 Unity提供了-个非常易用和强大的用于处理输入信息的类:Input,利用该类可以处理鼠标.键盘.摇杆/方向盘/手柄等游 ...
- Vue2.0 v-for 中 :key 的作用
- centos7配置网桥
在Centos7上玩KVM的话一定要配置网桥的: [root@localhost nb]# ls /etc/sysconfig/network-scripts ifcfg-8866 ifdown if ...
- 转:Git: git stash 用法小结
一.应用场景 综合下网上的介绍和资料, git stash (git储藏)可用于以下情形: 发现有一个类是多余的,想删掉它又担心以后需要查看它的代码,想保存它但又不想增加一个脏的提交.这时就可以考虑 ...
- .NET微信扫码支付模式二API接口开发测试
主要实现微信扫码支付,官网的SDKdemo 就不要使用 一直不能调试通过的,还是自己按照API接口文档一步一步来实现,吐槽下微信一点责任感都木有,能不能demo搞个正常的吗,不要坑惨了一大群码农们有点 ...
- spark提示Caused by: java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to [Lscala.collection.immutable.Map;
spark提示Caused by: java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot b ...
- GCC Reference
本文简单整理了GCC编译的命令项,可作为后续使用的参考. 编译 本文以GCC为主,默认编译*.c的c语言源代码. 源文件->可执行文件 gcc -Wall test.c -o test gcc ...