我现在正在学习LINQ基础知识,出现了一些问题。例如,我有一个整数列表-
var s = [3, 4, 5, 6, 1]
我不明白为什么我可以执行这个查询而没有错误:
s.Select((val, ind) => new { value = val, index = ind })
在List<int>
中,我们不能访问索引,Select
语句中的lambda函数如何理解ind
是索引器?
因为在Enumerable
类的Select
方法声明中清楚地提到了。
// selector:
// A transform function to apply to each source element; the second parameter of
// the function represents the index of the source element.
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);