我对这个问题很困惑,看不懂。在Enumerable
文档,我读过这个:
that implement System.Collections.Generic.IEnumerable
还有一些方法,比如 Select()
返回 IEnumerable<TSource>
我们可以使用其他方法,如 Where()
使用那个之后。例如:
names.Select(name => name).Where(name => name.Length > 3 );
但是Enumerable
不继承自 IEnumerable<T>
和 IEnumerable<T>
不包含 Select()
, Where()
等等...
我错了吗?
或者有什么原因吗?
最佳答案
Select()、Where() 等是“extension methods”。它们需要在“其他地方”定义,因为接口(interface)无法提供方法的实现。
您可以通过参数列表中的关键字“this”识别扩展方法。例如:
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
可以像 IEnumerable<TSource>
上的方法一样使用使用一个参数:Func<TSource, bool> predicate
.
关于c# - 为什么 Enumerable 不继承自 IEnumerable<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3004341/