淺談C#中Action和Func回調(diào)的常用方式
一、簡介
Action和Func泛型委托實際上就是一個.NET Framework預(yù)定義的委托,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
委托其實就是把方法當(dāng)作參數(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 表達(dá)式,效果一樣的,關(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)文章
基于WebRequest.RegisterPrefix的使用詳解
本篇文章對WebRequest.RegisterPrefix的使用進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
使用C#在Excel工作表中查找與替換數(shù)據(jù)的操作指南
在日常業(yè)務(wù)中,Excel 常用于保存產(chǎn)品清單、客戶信息、銷售記錄等結(jié)構(gòu)化數(shù)據(jù),本文將介紹如何使用 C# 在 Excel 中高效完成查找與替換操作,并結(jié)合一個實際業(yè)務(wù)場景表格數(shù)據(jù)進行演示,示例涵蓋從加載文件、查找目標(biāo)數(shù)據(jù)到替換與保存完整流程,需要的朋友可以參考下2025-11-11

