gpt4 book ai didi

c# - Func 是一个特殊的委托(delegate)还是我们也可以手动创建它?

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:31 26 4
gpt4 key购买 nike

Func<T, TResult>委托(delegate)用于具有单个参数和返回类型 TResult 的委托(delegate)。

这是一个特殊的委托(delegate)还是我们可以自己手动编码的东西?

例如我可以创建一个 Func<T, TResult> 吗?但是它接受 2 个输入参数?

最佳答案

"Func"实际上是 System 中的类命名空间,即:

Func<TResult> (0 个参数), Func<T,TResult> (1 个参数), Func<T1,T2,TResult> (2 个参数)... Func(17) (16 个参数)。

总共有 17 个不同的“Func”类,支持 0 到 16 个参数。允许它们在代码中共享名称(“Func”),即使它们实际上是不同的类型(FuncFunc'Func'' 等),这要归功于泛型的工作方式在 .NET 中——泛型的数量决定/消除使用哪个实际类型的歧义。

快乐编码。


就委托(delegate)之间的转换而言,这可能有用(在 LINQPad 中运行,“C# 程序”):

delegate int MyDelegate (int y);

void Main()
{
Func<int,int> fun1 = (q) => q * q;
MyDelegate del = new MyDelegate(fun1); // Convert the "Func" delegate to custom...
Func<int,int> fun2 = new Func<int,int>(del); // ...and back
}

关于c# - Func<T, TResult> 是一个特殊的委托(delegate)还是我们也可以手动创建它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com