有一个List<T>.FindIndex(Int32, Predicate <T>).这个方法正是我想要的IList<T>对象.
我知道IList有一个方法,IndexOf(T)但我需要谓词来定义比较算法.
有没有方法,扩展方法,LINQ或一些代码来查找一个项目的索引IList<T>?
Jon*_*eet 18
那么你可以很容易地编写自己的扩展方法:
public static int FindIndex<T>(this IList<T> source, int startIndex,
Predicate<T> match)
{
// TODO: Validation
for (int i = startIndex; i < source.Count; i++)
{
if (match(source[i]))
{
return i;
}
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3540 次 |
| 最近记录: |