淺談C#中Action和Func回調(diào)的常用方式
一、簡介
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("結(jié)算的結(jié)果是:" + res); } } public class Test1 { public void Calculation(int x, int y, Action<int> call) { if (call != null) { call(x + y); } } } }
運行:
將方法換成 Lambda 表達式,效果一樣的,關(guān)于Lambda的使用方法,可以參考:點擊跳轉(zhuǎn)
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("結(jié)算的結(jié)果是:" + res); }); Console.ReadKey(); } } public class Test1 { public void Calculation(int x, int y, Action<int> call) { 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<int, float, string> MyFunc = null 這個委托中,string 就是返回值,傳遞參數(shù)的時候,也只能傳遞兩個參數(shù),如果再多寫一個參數(shù)就會報錯,如下圖:
例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 Func<int, float, string> MyFunc = 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(); Func<string> func = () => { 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<string> sayFunc) { if(sayFunc != null) { int age = x + y; string msg = string.Format("年齡是:{0},對年齡的感受:{1}", age, sayFunc()); Console.WriteLine(msg); } } } }
運行:
上面代碼只是作為一個參考,讀者可以根據(jù)自己的需求做一個改進。
結(jié)束
到此這篇關(guān)于淺談C#中Action和Func回調(diào)的常用方式的文章就介紹到這了,更多相關(guān)C# Action和Func回調(diào)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實現(xiàn)將千分位字符串轉(zhuǎn)換成數(shù)字的方法
這篇文章主要介紹了C#實現(xiàn)將千分位字符串轉(zhuǎn)換成數(shù)字的方法,很適合初學者更好的理解C#字符串原理,需要的朋友可以參考下2014-08-08基于C#實現(xiàn)自定義計算的Excel數(shù)據(jù)透視表
數(shù)據(jù)透視表(Pivot?Table)是一種數(shù)據(jù)分析工具,通常用于對大量數(shù)據(jù)進行匯總、分析和展示,本文主要介紹了C#實現(xiàn)自定義計算的Excel數(shù)據(jù)透視表的相關(guān)知識,感興趣的可以了解下2023-12-12Unity3D實戰(zhàn)之答題系統(tǒng)的實現(xiàn)
本文將用Unity3D制作一個答題系統(tǒng),可以從文本文檔中提取題目和分數(shù),然后綁定到UI上,在答題的過程中,自動判斷分數(shù),自動判斷正確率。感興趣的可以學習一下2022-03-03C#中緩存System.Web.Caching用法總結(jié)
本文詳細講解了C#中緩存System.Web.Caching的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04