关于在执行查询时创建临时列表的c# LINQ问题



我现在正在学习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);

最新更新