網站首頁 編程語言 正文
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("----------------------------"); // 有參數無返回值的委托 Actionaction4 = 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() { 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 委托作為參數傳遞給ForEach()方法 list.ForEach(student => { Console.WriteLine($"姓名:{student.Name},年齡:{student.Age}"); }); } } }
3、在Main()方法中調用
ActionTest.Test();
4、結果
原文鏈接:https://www.cnblogs.com/dotnet261010/p/10108791.html
相關推薦
- 2022-09-13 Android實現屏幕旋轉四個方向準確監聽_Android
- 2022-04-17 論一次 taro小程序分包優化經歷,小程序體積過大的優化
- 2023-03-18 pandas檢查和填充缺失值的N種方法總結_python
- 2022-05-06 python?selenium中Excel數據維護指南_python
- 2022-04-23 uniapp用Promise封裝get和post請求
- 2023-05-21 Golang?flag包的具體使用_Golang
- 2022-09-13 c++實現排序算法之希爾排序方式_C 語言
- 2022-09-01 jquery實現全選功能_jquery
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支