简体   繁体   English

为什么Enumerable不从IEnumerable <T>继承

[英]Why Enumerable doesn't inherits from IEnumerable<T>

I'm very confused about this issue and can't understand it.In the Enumerable Documentation, I read this: 我对此问题非常困惑,无法理解。在Enumerable文档中,我读到了这个:

that implement System.Collections.Generic.IEnumerable 实现System.Collections.Generic.IEnumerable

and some methods like Select() return IEnumerable<TSource> that we can use from other methods like Where() after using that.for example: Select()类的一些方法返回IEnumerable<TSource> ,我们可以在使用之后从Where()等其他方法中使用它。例如:

names.Select(name => name).Where(name => name.Length > 3 );

but Enumerable doesn't inherit from IEnumerable<T> and IEnumerable<T> doesn't contain Select() , Where() and etc too... 但是Enumerable不继承IEnumerable<T>IEnumerable<T>也不包含Select()Where()等等......

have i wrong ? 我错了吗?
or exists any reason for this? 或存在任何理由?

The Select(), Where() etc are " extension methods ". Select(),Where()等是“ 扩展方法 ”。 They need to be defined "elsewhere" as an interface can't supply an implementation of methods. 它们需要在“其他地方”定义,因为接口不能提供方法的实现。

You can recognize extension methods by the keyword "this" in the argument list. 您可以通过参数列表中的关键字“this”识别扩展方法。 For instance: 例如:

public static IEnumerable<TSource> Where<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource, bool> predicate
)

can be used as if it's a method on an IEnumerable<TSource> with one parameter: Func<TSource, bool> predicate . 可以使用它作为IEnumerable<TSource>上的一个方法,带有一个参数: Func<TSource, bool> predicate

"why preferred extension methods against inheritance?" “为什么首选的扩展方法可以抵御继承?”

Enumerable is a static class that implements over 50 extension methods to IEnumerable. Enumerable是一个静态类,它为IEnumerable实现了50多种扩展方法。 This lets you use all those extension methods on types that implement IEnumerable without forcing the programmers to implement all those methods for each collection type. 这使您可以在实现IEnumerable的类型上使用所有这些扩展方法,而不必强制程序员为每个集合类型实现所有这些方法。 If Enumerable were an interface instead of a static class, each collection type (such as List, Dictionary, Set, etc) would have its own implementation of these extension methods. 如果Enumerable是一个接口而不是一个静态类,则每个集合类型(如List,Dictionary,Set等)都有自己的扩展方法实现。

Correct.but what about this sentence? 正确。但这句话怎么样?

that implement System.Collections.Generic.IEnumerable 实现System.Collections.Generic.IEnumerable

According to this sentence,We have to define methods in the IEnumerable<T> interface and implement in the Enumerable class by inheritance.is it correct? 根据这句话,我们必须在IEnumerable<T>接口中定义方法,并通过inheritance在Enumerable类中实现。它是否正确?

why preferred extension methods against inheritance? 为什么首选的扩展方法反对继承?

IEnumerable来自IEnumerable<T> ,这是一个2.0+接口。

A way to solve this is by casting the elements to their same type using Cast<T>() method, which returns and IEnumerable<T> version of the same elements. 解决此问题的方法是使用Cast<T>()方法将元素转换为相同的类型,该方法返回相同元素的IEnumerable<T>版本。

DataTable dt = ...
dt.Rows.Cast<DataRow>().Where()...

Rows is of type IEnumerable , and after casting it becomes of type IEnumerable<DataRow> , which is supported by LINQ extension methods. Rows的类型为IEnumerable ,并且在转换后它变为IEnumerable<DataRow>类型,LINQ扩展方法支持它。

我曾经说过你也读过这篇文章的迭代器,迭代器块和数据管道作者Jon Skeet的进一步见解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM