的问题是:为什么它使用此代码LINQ:为什么数组和列表使用不同的迭代器
if (source is TSource[])
return (IEnumerable<TResult>) new Enumerable.WhereSelectArrayIterator<TSource, TResult>((TSource[]) source, (Func<TSource, bool>) null, selector);
if (source is List<TSource>)
return (IEnumerable<TResult>) new Enumerable.WhereSelectListIterator<TSource, TResult>((List<TSource>) source, (Func<TSource, bool>) null, selector);
else
return (IEnumerable<TResult>) new Enumerable.WhereSelectEnumerableIterator<TSource, TResult>(source, (Func<TSource, bool>) null, selector);
}
,而不是
if (source is IList<TSource>)
return (IEnumerable<TResult>) new Enumerable.WhereSelectIListIterator<TSource, TResult>((List<TSource>) source, (Func<TSource, bool>) null, selector);
return (IEnumerable<TResult>) new Enumerable.WhereSelectEnumerableIterator<TSource, TResult>(source, (Func<TSource, bool>) null, selector);
}
我的意思是List<T>
和T[]
两个工具IList<T>
,他们都具有索引器并实现IEnumerable<T>
,因此它们都可以以相同的单数方式迭代,但现在使用不同的迭代器。
从哪里得到这些代码片段? – payo
我会想到,在使用这些迭代器时,LINQ可以做出合理的优化 - 并且可能不会以完全相同的方式迭代它们。但没有挖掘LINQ源,我不能证明这一点,只是一个强烈的预感(ergo,一条评论) – payo
@payo http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs –