在 C# 中使用 Func<T> 委托
Using Func<T> delegate in C#
也许我的问题太笼统了,但我会尽快总结一下。
几天来我一直在讲授委托(确切地说是 Func),但我无法理解一些事情:
我。案例一:
我有方法 Task.Run()
可以作为参数 Func<TResult>
。为什么,作为委托,使用 lambda 表达式,我可以传递一个也有参数的方法——在我看来,如果一个方法有参数,它与 Func 类型不兼容:
static void Main(string[] args)
{
// It's work and is ok
Task taskOne = Task.Run(calculateOne);
// It's work but why can i pass calculateTwo(2) as Func<TResult> ?If the parameter type is
// <TResult>, should the method not have any input parameters?
Task taskTwo = Task.Run(()=> calculateTwo(2));
}
public static int calculateOne()
{
return 1 + 9;
}
public static int calculateTwo(int t)
{
return 1 + 9;
}
二.案例二:
在第一个问题中,当方法参数为Func<Tresult>
时,我可以将参数作为委托传递给方法。我的第二个问题完全相反。为什么作为 Func <bool, int, int>
委托,我可以传递一个不带参数的方法?为什么我不能传递带参数的 lambda?
// Why this is ok, when BubleSort parameter type is Func<int,int,bool>?
// Shouldn't I put method parameters?
BubbleSort(GreatherThan);
// ERROR: Func <bool, int, int> means int and int are parameters, yet I can't pass
// parameters like this
BubbleSort(()=>GreatherThan(1,2));
public static void BubbleSort(Func<int,int,bool> compare)
{}
public static bool GreatherThan(int first, int second)
{
return first > second;
}
在这两种情况下,您编写的 lambda 表达式:
()=>GreatherThan(1,2)
()=> calculateTwo(2)
表示没有参数的函数,如空括号()
所示。在 lambda 表达式 中做什么并不重要。 () => calculateTwo(2)
表示一个没有参数的函数,它调用 calculateTwo(2)
作为它唯一做的事情。想象一下:
int AnonymousFunction1() {
return calculateTwo(2);
}
bool AnonymousFunction2() {
return GreaterThan(1, 2);
}
把这些想象成你自己 实际上 通过编写那些 lambda 表达式传递给 Task.Run
。这些函数显然兼容Func<int>
和Func<bool>
,不兼容Func<int, int, bool>
.
Func不接受任何参数,所以可以写lambda
像这样
() => calculateTwo(1)
并且符合 Func 签名
要对 Func 做同样的事情,你需要这样写 (first, second) => GreatherThan(1,2)
但这是 Func 的错误用法。它旨在为任何其他代码传递方法引用,可以像那样调用它
public static void BubbleSort(Func<int,int,bool> compare)
{
compare(1,2); // or something else
}
对不起我的语言:)
也许我的问题太笼统了,但我会尽快总结一下。
几天来我一直在讲授委托(确切地说是 Func),但我无法理解一些事情:
我。案例一:
我有方法 Task.Run()
可以作为参数 Func<TResult>
。为什么,作为委托,使用 lambda 表达式,我可以传递一个也有参数的方法——在我看来,如果一个方法有参数,它与 Func 类型不兼容:
static void Main(string[] args)
{
// It's work and is ok
Task taskOne = Task.Run(calculateOne);
// It's work but why can i pass calculateTwo(2) as Func<TResult> ?If the parameter type is
// <TResult>, should the method not have any input parameters?
Task taskTwo = Task.Run(()=> calculateTwo(2));
}
public static int calculateOne()
{
return 1 + 9;
}
public static int calculateTwo(int t)
{
return 1 + 9;
}
二.案例二:
在第一个问题中,当方法参数为Func<Tresult>
时,我可以将参数作为委托传递给方法。我的第二个问题完全相反。为什么作为 Func <bool, int, int>
委托,我可以传递一个不带参数的方法?为什么我不能传递带参数的 lambda?
// Why this is ok, when BubleSort parameter type is Func<int,int,bool>?
// Shouldn't I put method parameters?
BubbleSort(GreatherThan);
// ERROR: Func <bool, int, int> means int and int are parameters, yet I can't pass
// parameters like this
BubbleSort(()=>GreatherThan(1,2));
public static void BubbleSort(Func<int,int,bool> compare)
{}
public static bool GreatherThan(int first, int second)
{
return first > second;
}
在这两种情况下,您编写的 lambda 表达式:
()=>GreatherThan(1,2)
()=> calculateTwo(2)
表示没有参数的函数,如空括号()
所示。在 lambda 表达式 中做什么并不重要。 () => calculateTwo(2)
表示一个没有参数的函数,它调用 calculateTwo(2)
作为它唯一做的事情。想象一下:
int AnonymousFunction1() {
return calculateTwo(2);
}
bool AnonymousFunction2() {
return GreaterThan(1, 2);
}
把这些想象成你自己 实际上 通过编写那些 lambda 表达式传递给 Task.Run
。这些函数显然兼容Func<int>
和Func<bool>
,不兼容Func<int, int, bool>
.
Func不接受任何参数,所以可以写lambda
像这样
() => calculateTwo(1)
并且符合 Func 签名
要对 Func(first, second) => GreatherThan(1,2)
但这是 Func 的错误用法。它旨在为任何其他代码传递方法引用,可以像那样调用它
public static void BubbleSort(Func<int,int,bool> compare)
{
compare(1,2); // or something else
}
对不起我的语言:)