是的,我看到了这个问题,但我找不到我的具体问题的答案。
给定一个接受T并返回布尔值的lambda testLambda(我可以将其制作为Predicate或Func,这取决于我)
我需要能够同时使用List.FindIndex(testLambda)(采用Predicate)和List.Where(testLambda)(采用Func)。
有什么想法如何同时实现两者?
简单:
Func<string,bool> func = x => x.Length > 5;
Predicate<string> predicate = new Predicate<string>(func);
基本上,您可以使用任何兼容的现有实例创建新的委托实例。这也支持协变和逆变:
Action<object> actOnObject = x => Console.WriteLine(x);
Action<string> actOnString = new Action<string>(actOnObject);
Func<string> returnsString = () => "hi";
Func<object> returnsObject = new Func<object>(returnsString);
static Predicate<T> ConvertToPredicate<T>(Func<T, bool> func)
{
return new Predicate<T>(func);
}
我得到了这个:
Func<object, bool> testLambda = x=>true;
int idx = myList.FindIndex(x => testLambda(x));
可以工作,但感觉不太好。
我有点晚加入游戏,但我喜欢扩展方法:
public static class FuncHelper
{
public static Predicate<T> ToPredicate<T>(this Func<T,bool> f)
{
return x => f(x);
}
}
List<int> list = new List<int> { 1, 3, 4, 5, 7, 9 };
Func<int, bool> isEvenFunc = x => x % 2 == 0;
var index = list.FindIndex(isEvenFunc.ToPredicate());
嗯,我现在看到了FindIndex扩展方法。这可能是一个更加通用的答案。与ConvertToPredicate并没有太大区别。
听起来像是一个案例
static class ListExtensions
{
public static int FindIndex<T>(this List<T> list, Func<T, bool> f) {
return list.FindIndex(x => f(x));
}
}
// ...
Func<string, bool> f = x=>Something(x);
MyList.FindIndex(f);
// ...
我喜欢C#3 ...
Predicate<T>
的构造函数在哪里有说明文档?MSDN似乎只提到可以通过Lambda表达式或方法创建Predicate<T>
,从未提到过使用new
。 - gilly3Predicate<T>
构造函数 - 它只是一个委托类型,可以像任何其他委托一样构造。 - Jon Skeetnew
创建委托。它展示了后来版本.Net的更新语法。但是,Visual Studio告诉我不能在两个委托之间使用此更新的语法(例如Func<T, bool>
和Predicate<T>
)。显然,我必须使用new
。在MSDN上几乎没有关于使用委托类型的new
的文档 - 我找不到将委托实例作为参数传递的示例。 C#规范中的第7.6.10.5节包含我正在寻找的内容,但这并不令人满意。 - gilly3