網(wǎng)站首頁 編程語言 正文
一、簡介
Action和Func泛型委托實際上就是一個.NET Framework預定義的委托,3.5引入的特性?;竞w了所有常用的委托,所以一般不用用戶重新聲明。Action系列泛型委托,是沒有返回參數(shù)的委托,最多可以有16參數(shù),也可以沒有參數(shù)。
Func系列的委托是有返回值的委托,最多可以有16個參數(shù);元組是C# 4.0引入的一個新特性,編寫的時候需要基于.NET Framework 4.0或者更高版本。元組使用泛型來簡化一個類的定義.提供用于創(chuàng)造元組對象的靜態(tài)方法。最多可以提供創(chuàng)建新的 8 元組,即八元組。
二、Action
委托其實就是把方法當作參數(shù)來調(diào)用,Action就是其中之一,Action 作為參數(shù)不能有返回值,參數(shù)可以是任意類型,也可以不傳遞參數(shù)。
例1
調(diào)用某個類中的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常用的方式通常是用來作為和回調(diào)?
例2
執(zhí)行一系列的操作后,再執(zhí)行回調(diào),也是比較推薦的使用方式。
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的案例中,執(zhí)行回調(diào)后,都沒有返回值,這是因為Action并不能接收返回值,如果想執(zhí)行回調(diào),又有返回值怎么辦呢,F(xiàn)unc就是用來解決這個問題的。
Func 必須有一個返回值,否則會報錯,如下圖:
返回值通常是在參數(shù)的最后一個,參考例1,F(xiàn)unc
例1
基本的用法,func賦值,執(zhí)行委托,并接收返回值
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作為方法的參數(shù)傳遞,并執(zhí)行回調(diào)
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); } } } }
運行:
上面代碼只是作為一個參考,讀者可以根據(jù)自己的需求做一個改進。
結束
原文鏈接:https://blog.csdn.net/qq_38693757/article/details/123041382
相關推薦
- 2022-05-24 Django基礎CBV裝飾器和中間件的應用示例_python
- 2022-09-13 Android四大組件之Activity深入解讀生命周期_Android
- 2022-08-05 C++詳細講解模擬實現(xiàn)位圖和布隆過濾器的方法_C 語言
- 2022-04-05 Linux環(huán)境 redis 值中文顯示亂碼 解決辦法 --raw參數(shù)
- 2023-04-02 go?MethodByName()不能獲取私有方法的解決_Golang
- 2022-12-01 Git基礎學習之分支操作的示例詳解_相關技巧
- 2023-02-12 python語音信號處理詳細教程_python
- 2023-06-17 Python結合Sprak實現(xiàn)計算曲線與X軸上方的面積_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支