任何想法如何在列表上进行这样的查找?或者,如果没有编写迭代器并手动执行查找,这是不可能的?在这种情况下,或许有一个关键的集合我应该用来代替?
private List<CustomClass> files; public voID someMethod() { Uri u= new Uri(www.test.com); CustomClass cc = this.files.find( matchesUri(u) ); // WON'T LET ME DO THIS } private static bool matchesUri(List<CustomClass> cc,Uri _u) { return cc.Path == _u; }public class CustomClass{ private Uri path; public Uri Path { get { return this.path; } set { this.path = value; } }}
PS.我必须承认我在http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx的doco中并没有完全遵循谓词
解决方法 使用lambda:Uri u = new Uri("www.test.com"); CustomClass cc = this.files.Find(cc => cc.Path == u);
或者如果您仍想要一个命名方法:
static bool matchesUri(CustomClass cc,Uri _u){ return cc.Path == _u;} Uri u = new Uri("www.test.com"); CustomClass cc = this.files.Find(cc => matchesUri(cc,u));总结
以上是内存溢出为你收集整理的C#List.Find方法 – 如何将值传递给谓词?全部内容,希望文章能够帮你解决C#List.Find方法 – 如何将值传递给谓词?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)