Benefit of Enumerables

1.4k views Asked by At

I am struggling to understand the benefit of IEnumerable. I understand that Enumerables allow execution to be deferred to later on.

Please see the example here: 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. Therefore this appears to eliminate the advantage of deferring the execution until later on. What am I missing here?

4

There are 4 answers

7
Patrick Hofman On BEST ANSWER

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

0
AVS On

It is a design choice whether to defer the evaluation or not, but is not mandated by IEnumerable. Some Linq output queries(like Where & Select) defer the evaluation, other collections like Dictionary or List don't.

You should use an IEnumerable when the only information you need from the collection is to be able to 'enumerate' the elements.

Let's say you want to implement a ToString method for a generic collection, where the object has its own ToString. If you accept an IEnumerable instead of an array, a List or a Set can be passed as argument, without any code changes.

5
Sefe On

The main purpose of an IEnumerable is not to defer execution until later. The purpose of an IEnumerable is to provide a way to iterate over the parts of an object. These parts can be the items of a list, but they can also be (for example) the characters of a 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.

The main and most important consumer of an IEnumerable is the foreach statement, which is there since the beginning of time.NET. Only later came LINQ with the Enumerable class that also builds on the IEnumerable (or more precisely on the IEnumerable<T>) interface.

Speaking of LINQ, the deferred execution you refer to in your question indeed is a key feature of LINQ. It means that a query that you define is not executed until the iteration of the IEnumerable is performed. 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. 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.

1
Amir Popovich On

One example of IEnumerable is late execution.

I thing that the bigs advantage of using an IEnumerable are:

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