網站首頁 編程語言 正文
一、簡介
Action和Func泛型委托實際上就是一個.NET Framework預定義的委托,3.5引入的特性。基本涵蓋了所有常用的委托,所以一般不用用戶重新聲明。Action系列泛型委托,是沒有返回參數的委托,最多可以有16參數,也可以沒有參數。
Func系列的委托是有返回值的委托,最多可以有16個參數;元組是C# 4.0引入的一個新特性,編寫的時候需要基于.NET Framework 4.0或者更高版本。元組使用泛型來簡化一個類的定義.提供用于創造元組對象的靜態方法。最多可以提供創建新的 8 元組,即八元組。
二、Action
委托其實就是把方法當作參數來調用,Action就是其中之一,Action 作為參數不能有返回值,參數可以是任意類型,也可以不傳遞參數。
例1
調用某個類中的Action
using System; namespace Test1 { internal class Program { static void Main(string[] args) { Test1 test1 = new Test1(); test1.myAction(); Console.ReadKey(); } } public class Test1 { public Action myAction = null; private void sayHi() { Console.WriteLine("fuck you!"); } public Test1() { myAction = sayHi; } } }
運行:
這種方式用的比較少,Action常用的方式通常是用來作為和回調?
例2
執行一系列的操作后,再執行回調,也是比較推薦的使用方式。
using System; namespace Test1 { internal class Program { static void Main(string[] args) { Test1 test1 = new Test1(); test1.Calculation(1, 2, ReceiveResult); Console.ReadKey(); } private static void ReceiveResult(int res) { Console.WriteLine("結算的結果是:" + res); } } public class Test1 { public void Calculation(int x, int y, Actioncall) { if (call != null) { call(x + y); } } } }
運行:
將方法換成?Lambda 表達式,效果一樣的,關于Lambda的使用方法,可以參考:點擊跳轉
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test1 { internal class Program { static void Main(string[] args) { Test1 test1 = new Test1(); test1.Calculation(1, 2, (int res) => { Console.WriteLine("結算的結果是:" + res); }); Console.ReadKey(); } } public class Test1 { public void Calculation(int x, int y, Actioncall) { if (call != null) { call(x + y); } } } }
三、Func
上面使用Action的案例中,執行回調后,都沒有返回值,這是因為Action并不能接收返回值,如果想執行回調,又有返回值怎么辦呢,Func就是用來解決這個問題的。
Func 必須有一個返回值,否則會報錯,如下圖:
返回值通常是在參數的最后一個,參考例1,Func
例1
基本的用法,func賦值,執行委托,并接收返回值
using System; namespace Test1 { internal class Program { static void Main(string[] args) { Test1 test1 = new Test1(); string userName = test1.MyFunc(15, 180.2f); Console.WriteLine(userName); Console.ReadKey(); } } public class Test1 { public FuncMyFunc = null; private string GetUserName(int age, float height) { if (age == 15 && height == 180.2f) { return "張三"; } return null; } public Test1() { MyFunc = GetUserName; } } }
運行:
例2
?把func作為方法的參數傳遞,并執行回調
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test1 { internal class Program { static void Main(string[] args) { Test1 test1 = new Test1(); Funcfunc = () => { string name = "張三"; string feel = "精力非常旺盛"; string msg = name + feel; return msg; }; test1.Calculation(10, 12, func); Console.ReadKey(); } } public class Test1 { public void Calculation(int x,int y, Func sayFunc) { if(sayFunc != null) { int age = x + y; string msg = string.Format("年齡是:{0},對年齡的感受:{1}", age, sayFunc()); Console.WriteLine(msg); } } } }
運行:
上面代碼只是作為一個參考,讀者可以根據自己的需求做一個改進。
結束
原文鏈接:https://blog.csdn.net/qq_38693757/article/details/123041382
相關推薦
- 2022-12-05 C++?Boost?Heap使用實例詳解_C 語言
- 2022-07-17 SQL?Server格式轉換函數Cast、Convert介紹_MsSql
- 2024-03-28 存儲過程整合springboot
- 2022-09-23 深入了解C++的多態與虛函數_C 語言
- 2023-01-23 python操作excel之openpyxl模塊讀寫xlsx格式使用方法詳解_python
- 2022-01-27 worker process terminated 進程退出
- 2023-07-07 Linux服務器zip安裝,及壓縮解壓
- 2022-07-15 C++面向對象之類和對象那些你不知道的細節原理詳解_C 語言
- 最近更新
-
- 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同步修改后的遠程分支