简体   繁体   English

Enumerables的好处

[英]Benefit of Enumerables

I am struggling to understand the benefit of IEnumerable. 我正在努力了解IEnumerable的好处。 I understand that Enumerables allow execution to be deferred to later on. 我知道Enumerables允许将执行推迟到以后。

Please see the example here: https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx . 请参阅此处的示例: https//msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx In this example, the array is already populated in the main class and then injected into an IEnumerable class. 在此示例中,数组已在主类中填充,然后注入IEnumerable类。 Therefore this appears to eliminate the advantage of deferring the execution until later on. 因此,这似乎消除了延迟执行直到以后的优点。 What am I missing here? 我在这里错过了什么?

The main purpose of an IEnumerable is not to defer execution until later. IEnumerable的主要目的不是将执行推迟到以后。 The purpose of an IEnumerable is to provide a way to iterate over the parts of an object. IEnumerable的目的是提供一种迭代对象部分的方法。 These parts can be the items of a list, but they can also be (for example) the characters of a string . 这些部分可以是列表中的项目,但它们也可以是(例如) string The IEnumerable interface exists because not every class that allows iteration over its parts is a collection or a list (such as a string ) and IEnumerable provides a minimal interface to achieve this without having to implement a full blown ICollection or IList . 存在IEnumerable接口,因为不是每个允许迭代其部分的类都是集合或列表(例如string ),并且IEnumerable提供了一个最小的接口来实现这一点,而无需实现完整的ICollectionIList

The main and most important consumer of an IEnumerable is the foreach statement, which is there since the beginning of time .NET. 一个主要的和最重要的消费IEnumerableforeach语句,它是存在的,因为 时间 .NET的开始。 Only later came LINQ with the Enumerable class that also builds on the IEnumerable (or more precisely on the IEnumerable<T> ) interface. 之后才出现了带有Enumerable类的LINQ,该类也构建在IEnumerable (或者更准确地说是在IEnumerable<T> )接口上。

Speaking of LINQ, the deferred execution you refer to in your question indeed is a key feature of LINQ. 说到LINQ,你在问题中提到的延迟执行确实是LINQ的一个关键特性。 It means that a query that you define is not executed until the iteration of the IEnumerable is performed. 这意味着在执行IEnumerable的迭代之前,不会执行您定义的查询。 This allows you to further refine your queries before you execute them. 这允许您在执行查询之前进一步优化查询。

This deferred execution though only refers to how the IEnumerable is used , not to what it does . 这种延迟执行虽然只指如何 IEnumerable 使用 ,而不是它做什么 Deferred execution means that you will not start to iterate over the IEnumerable when you define it, but at a different point in the program, for example a foreach , a ToList etc. 延迟执行意味着您在定义它时不会开始迭代IEnumerable ,而是在程序中的不同点,例如foreachToList等。

The documentation you reference is merely a how could you implement it , rather than the most efficient way . 您引用的文档仅仅是您如何实现它 ,而不是最有效的方式 Of course, the implementation shown has no use at all, since as you say the array is already constructed and enumerable on itself. 当然,所示的实现完全没有用,因为正如你所说的那样,数组已经构建并且可以枚举。

The sample is useful at best to show how a custom type can act like a collection, by implementing IEnumerable . 通过实现IEnumerable ,该示例最多可用于显示自定义类型如何像集合一样工作。 This very generic interface can of course be used to be called from a variety of places where you don't need specific knowledge about the collection type, but that isn't shown in the sample. 这个非常通用的接口当然可以用于从您不需要有关集合类型的特定知识的各种地方调用,但样本中未显示。

An enumerator is used to iterate over a set of data from begin to end. 枚举器用于从头到尾迭代一组数据。 You can't read the previous item back since it can already be discarded (for example an iterator that needs to download very large files from a remote location, you might not want to cache them). 您无法读取之前的项目,因为它已经被丢弃(例如,需要从远程位置下载非常大的文件的迭代器,您可能不想缓存它们)。

One example of IEnumerable is late execution. IEnumerable的一个例子是延迟执行。

I thing that the bigs advantage of using an IEnumerable are: 我认为使用IEnumerable的大优势是:

  1. You can iterate a collection without knowing how to iterate it (does the collection have an indexer? How should I iterate a Tree? etc.). 您可以在不知道如何迭代它的情况下迭代集合(集合是否有索引器?我应该如何迭代树?等等)。

  2. You can iterate a part of a collection - Think about finding the first element that meets your condition. 您可以迭代集合的一部分 - 考虑找到符合您条件的第一个元素。

  3. Late execution - You can filter collections and the real filtering will occur as soon as you traverse your IEnumerable (using ForEach\\ToList etc.). 延迟执行 - 您可以过滤集合,并且只要遍历IEnumerable(使用ForEach \\ ToList等)就会发生真正的过滤。

It is a design choice whether to defer the evaluation or not, but is not mandated by IEnumerable. 是否推迟评估是一种设计选择,但IEnumerable并未强制要求。 Some Linq output queries(like Where & Select) defer the evaluation, other collections like Dictionary or List don't. 一些Linq输出查询(如Where&Select)推迟评估,其他集合(如Dictionary或List)则不会。

You should use an IEnumerable when the only information you need from the collection is to be able to 'enumerate' the elements. 当集合中需要的唯一信息是能够“枚举”元素时,您应该使用IEnumerable。

Let's say you want to implement a ToString method for a generic collection, where the object has its own ToString. 假设您要为泛型集合实现ToString方法,其中对象具有自己的ToString。 If you accept an IEnumerable instead of an array, a List or a Set can be passed as argument, without any code changes. 如果接受IEnumerable而不是数组,则可以将List或Set作为参数传递,而不进行任何代码更改。

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

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