Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between IEnumerable and arrays?

Tags:

c#

ienumerable

Will anyone describe IEnumerable and what is difference between IEnumerable and array and where to use it.. all information about it and how to use it.

like image 446
Nishant Kumar Avatar asked Aug 26 '10 16:08

Nishant Kumar


People also ask

Is IEnumerable an array?

All arrays implement IList, and IEnumerable. You can use the foreach statement to iterate through an array.

Is IEnumerable list or array?

This interface enables iterating over a collection. In other words, if something is an IEnumerable , you can mostly think of it like an array or a list. You can use a foreach statement to loop through it, you can use LINQ to map or reduce it in a hundred different ways, or you can explicitly cast it to an array with .

What is the difference between IEnumerable and list?

IEnumerable is a deferred execution while List is an immediate execution. IEnumerable will not execute the query until you enumerate over the data, whereas List will execute the query as soon as it's called. Deferred execution makes IEnumerable faster because it only gets the data when needed.

What is a IEnumerable?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.


2 Answers

An array is a collection of objects with a set size.

int[] array = [0, 1, 2];

This makes it very useful in situations where you may want to access an item in a particular spot in the collection since the location in memory of each element is already known

array[1];

Also, the size of the array can be calculated quickly.

IEnumerable, on the other hand, basically says that given a start position it is possible to get the next value. One example of this may be an infinite series of numbers:

public IEnumerable<int> Infinite()
{
    int i = 0;
    while(true)
        yield return i++;
}

Unlike an array an enumerable collection can be any size and it is possible to create the elements as they are required, rather than upfront, this allows for powerful constructs and is used extensively by LINQ to facilitate complex queries.

//This line won't do anything until you actually enumerate the created variable
IEnumerable<int> firstTenOddNumbers = Infinite().Where(x => x % 2 == 1).Take(10);

However the only way to get a specific element is to start at the beginning and enumerate through to the one you want. This will be considerably more expensive than getting the element from a pre-generated array.

Of course you can enumerate through an array, so an array implements the IEnumerable interface.

like image 109
Martin Harris Avatar answered Oct 02 '22 19:10

Martin Harris


.NET has its IEnumerable interface misnamed - it should be IIterable. Basically a System.Collection.IEnumerable or (since generics) System.Collection.Generic.IEnumerable allows you to use foreach on the object implementing these interfaces.

(Side note: actually .NET is using duck typing for foreach, so you are not required to implement these interfaces - it's enough if you provide the suitable method implementations.)

An array (System.Array) is a type of a sequence (where by sequence I mean an iterable data structure, i.e. anything that implements IEnumerable), with some important differences.

For example, an IEnumerable can be - and is often - lazy-loaded. That means that until you explicitly iterate over it, the items won't be created. This can lead to strange behaviour if you're not aware of it. As a consequence, an IEnumerable has no means of telling you how many items it contains until you actually iterate over it (which the Count extension method in System.Linq.Enumerable class does).

An array has a Length property, and with this we have arrived to the most important difference: an array if a sequence of fixed (and known) items. It also provides an indexer, so you can conveniently access its items without actually iterating over it.

And just for the record, the "real" enumerations in .NET are types defined with the enum keyword. They allow you express a choices without using magic numbers or strings. They can be also used as flags, when marked with the FlagsAttribute.

I suggest you to use your favioure search engine to get more details about these concepts - my brief summary clearly doesn't aim to provide a deep insight to these features.

like image 44
ShdNx Avatar answered Oct 02 '22 19:10

ShdNx