網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1、什么是Action泛型委托
Action
2、Action委托定義
查看Action的定義:
using System.Runtime.CompilerServices; namespace System { // // 摘要: // 封裝一個(gè)方法,該方法不具有參數(shù)且不返回值。 [TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")] public delegate void Action(); }
你會(huì)發(fā)現(xiàn),Action其實(shí)就是沒(méi)有返回值的delegate。
3、示例
Action委托至少0個(gè)參數(shù),至多16個(gè)參數(shù),無(wú)返回值。
Action 表示無(wú)參,無(wú)返回值的委托。
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) { // 無(wú)參數(shù)無(wú)返回值的委托 Action action1 = new Action(ActionWithNoParaNoReturn); action1(); Console.WriteLine("----------------------------"); // 使用delegate Action action2 = delegate { Console.WriteLine("這里是使用delegate"); }; // 執(zhí)行 action2(); Console.WriteLine("----------------------------"); // 使用匿名委托 Action action3 = () => { Console.WriteLine("這里是匿名委托"); }; action3(); Console.WriteLine("----------------------------"); // 有參數(shù)無(wú)返回值的委托 Actionaction4 = new Action (ActionWithPara); action4(23); Console.WriteLine("----------------------------"); // 使用delegate Action action5 = delegate (int i) { Console.WriteLine($"這里是使用delegate的委托,參數(shù)值是:{i}"); }; action5(45); Console.WriteLine("----------------------------"); // 使用匿名委托 Action action6 = (string s) => { Console.WriteLine($"這里是使用匿名委托,參數(shù)值是:{s}"); }; action6("345"); Console.WriteLine("----------------------------"); // 多個(gè)參數(shù)無(wú)返回值的委托 Action action7 = new Action (ActionWithMulitPara); action7(7, "abc"); Console.WriteLine("----------------------------"); // 使用delegate Action action8 = delegate (int i1, int i2, string s) { Console.WriteLine($"這里是三個(gè)參數(shù)的Action委托,參數(shù)1的值是:{i1},參數(shù)2的值是:{i2},參數(shù)3的值是:{s}"); }; action8(12, 34, "abc"); Console.WriteLine("----------------------------"); Action action9 = (int i1,int i2, string s1,string s2) => { Console.WriteLine($"這里是使用四個(gè)參數(shù)的委托,參數(shù)1的值是:{i1},參數(shù)2的值是:{i2},參數(shù)3的值是:{s1},參數(shù)4的值是:{s2}"); }; // 執(zhí)行委托 action9(34,56, "abc","def"); Console.ReadKey(); } static void ActionWithNoParaNoReturn() { Console.WriteLine("這是無(wú)參數(shù)無(wú)返回值的Action委托"); } static void ActionWithPara(int i) { Console.WriteLine($"這里是有參數(shù)無(wú)返回值的委托,參數(shù)值是:{i}"); } static void ActionWithMulitPara(int i,string s) { Console.WriteLine($"這里是有兩個(gè)參數(shù)無(wú)返回值的委托,參數(shù)1的值是:{i},參數(shù)2的值是:{s}"); } } }
運(yùn)行結(jié)果:
4、真實(shí)示例
先看下面一張截圖:
從截圖中可以看出:ForEach()方法的參數(shù)是一個(gè)參數(shù)類型是T的無(wú)返回值的Action委托,下面的示例中利用Action委托作為參數(shù)傳遞給ForEach()方法。
1、定義Student實(shí)體類
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()方法輸出集合內(nèi)容
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() { Listlist = 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 委托作為參數(shù)傳遞給ForEach()方法 list.ForEach(student => { Console.WriteLine($"姓名:{student.Name},年齡:{student.Age}"); }); } } }
3、在Main()方法中調(diào)用
ActionTest.Test();
4、結(jié)果
原文鏈接:https://www.cnblogs.com/dotnet261010/p/10108791.html
相關(guān)推薦
- 2022-06-26 Go語(yǔ)言開(kāi)源庫(kù)實(shí)現(xiàn)Onvif協(xié)議客戶端設(shè)備搜索_Golang
- 2022-03-09 軟件構(gòu)建工具makefile基礎(chǔ)講解_C 語(yǔ)言
- 2023-02-15 Qt槽函數(shù)會(huì)被執(zhí)行多次的問(wèn)題原因及解決方法_C 語(yǔ)言
- 2021-11-27 關(guān)于UDP服務(wù)器客戶端編程流程介紹_C 語(yǔ)言
- 2022-08-30 C語(yǔ)言深入詳解四大內(nèi)存函數(shù)的使用_C 語(yǔ)言
- 2021-12-15 由于redis服務(wù)器cpu100%的問(wèn)題導(dǎo)致網(wǎng)站宕機(jī)訪問(wèn)大量出現(xiàn)504gateway time-ou
- 2022-08-11 C#中的composite模式示例詳解_C#教程
- 2022-09-04 關(guān)于Python下的Matlab函數(shù)對(duì)應(yīng)關(guān)系(Numpy)_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支