C#内置泛型委托中Action委托是什么
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,小编给大家分享一下C#内置泛型委托中Action委托是什么,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1、什么是Acti
千家信息网最后更新 2026年10月31日C#内置泛型委托中Action委托是什么
小编给大家分享一下C#内置泛型委托中Action委托是什么,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
1、什么是Action泛型委托
Action
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
Action
Action

代码示例如下:
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 action4 = new Action(ActionWithPara); action4(23); Console.WriteLine("----------------------------"); // 使用delegate Action action5 = delegate (int i) { Console.WriteLine($"这里是使用delegate的委托,参数值是:{i}"); }; action5(45); Console.WriteLine("----------------------------"); // 使用匿名委托 Action action6 = (string s) => { Console.WriteLine($"这里是使用匿名委托,参数值是:{s}"); }; action6("345"); Console.WriteLine("----------------------------"); // 多个参数无返回值的委托 Action action7 = new Action(ActionWithMulitPara); action7(7, "abc"); Console.WriteLine("----------------------------"); // 使用delegate Action action8 = delegate (int i1, int i2, string s) { Console.WriteLine($"这里是三个参数的Action委托,参数1的值是:{i1},参数2的值是:{i2},参数3的值是:{s}"); }; action8(12, 34, "abc"); Console.WriteLine("----------------------------"); Action 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实体类
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 list = new List() { 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委托作为参数传递给ForEach()方法 list.ForEach(student => { Console.WriteLine($"姓名:{student.Name},年龄:{student.Age}"); }); } }} 3、在Main()方法中调用
ActionTest.Test();
4、结果
以上是"C#内置泛型委托中Action委托是什么"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
委托
参数
方法
示例
内容
篇文章
封装
C#
截图
结果
三个
不怎么
不用
两个
也就是
也就是说
代码
多个
大部分
姓名
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
花魁咖啡
互联网是第几次科技革命的成果
涨乐互联网科技
苏州互联网科技贷款公司
互联网 新科技
无锡溥仪互联网科技有限公司
互联网科技创新企业排名
哈尔滨秒房互联网科技有限公司
互联网科技博主是什么意思
互联网教育科技公司vipkid
中国的互联网科技企业的崛起
互联网与医疗科技
开一家科技互联网公司需要多少钱
杭州披云加互联网科技有限公司
产业互联网科技服务公司
张召忠谈中国互联网科技
浙南科技城互联网大厦招聘
神州科技互联网有限公司
亚光科技低轨卫星互联网
科技互联网企业在行动
圆梦互联网科技
北京互联网科技
青岛互联网科技公司
互联网科技生活的内容
互联网科技博主是什么意思
互联网为啥属于高科技
中天科技中国工业互联网
广州百诚互联网科技有限公司官网
21世纪互联网科技成就
广州全民伙伴互联网科技有限公司