无法理解如何编码 Func<Func<T1, T2>, T3>
Can't understand how to code Func<Func<T1, T2>, T3>
我有这个方法:
public static IQueryable<char> Test(this string text, Func<Func<char, bool>, int> func)
这里char是要在文本中查找的字符,bool判断是否找到字符,intreturns字符在文本中的索引
这段代码应该怎么写?谢谢
下面是一个示例,可以让您了解它的作用。
我不知道你为什么需要这个,但可能是有原因的:)。
// These Func's take a char and return a boolean value.
Func<char, bool> f1 = (ch) => ch == 'a';
Func<char, bool> f2 = (ch) => ch == 'b';
char chr = 'a';
// This Func takes a function (of the type we saw above) and returns an integer.
Func<Func<char, bool>, int> func = (foo) => foo(chr) ? 1 : 0;
// Run the complex Func by passing a function as an input param and receiving an integer as a response.
int res1 = func(f1); // 1
int res2 = func(f2); // 0
根据您的要求,这是另一个例子(我仍然找不到好的用法,但无论如何):
string text = "TesTinG";
Func<char, bool> IsCapital = ch => ch == char.ToUpper(ch);
int counter = 0;
foreach (char chr in text.ToCharArray())
{
Func<Func<char, bool>, int> func = fn => fn(chr) ? 1 : 0;
counter += func(IsCapital);
}
我有这个方法:
public static IQueryable<char> Test(this string text, Func<Func<char, bool>, int> func)
这里char是要在文本中查找的字符,bool判断是否找到字符,intreturns字符在文本中的索引
这段代码应该怎么写?谢谢
下面是一个示例,可以让您了解它的作用。
我不知道你为什么需要这个,但可能是有原因的:)。
// These Func's take a char and return a boolean value.
Func<char, bool> f1 = (ch) => ch == 'a';
Func<char, bool> f2 = (ch) => ch == 'b';
char chr = 'a';
// This Func takes a function (of the type we saw above) and returns an integer.
Func<Func<char, bool>, int> func = (foo) => foo(chr) ? 1 : 0;
// Run the complex Func by passing a function as an input param and receiving an integer as a response.
int res1 = func(f1); // 1
int res2 = func(f2); // 0
根据您的要求,这是另一个例子(我仍然找不到好的用法,但无论如何):
string text = "TesTinG";
Func<char, bool> IsCapital = ch => ch == char.ToUpper(ch);
int counter = 0;
foreach (char chr in text.ToCharArray())
{
Func<Func<char, bool>, int> func = fn => fn(chr) ? 1 : 0;
counter += func(IsCapital);
}