简体   繁体   中英

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:

that implement 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:

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...

have i wrong ?
or exists any reason for this?

The Select(), Where() etc are " extension methods ". 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. 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 .

"why preferred extension methods against inheritance?"

Enumerable is a static class that implements over 50 extension methods to IEnumerable. 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. 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.

Correct.but what about this sentence?

that implement 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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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