What is the difference between IEnumerable and Array, IList and List?

What is the difference between IEnumerable and Array ?

What is the difference between IList and List ?

They seem to have the same function.

+75
c #
Apr 19 '09 at 2:58
source share
7 answers

IEnumerable provides only minimal "iterable" functionality. You can go through the sequence, but more on that. This has drawbacks - for example, it is very inefficient to read elements using IEnumerable or getting the nth element - but it also has advantages - for example, IEnumerable can be an infinite sequence, such as a sequence of primes.

Array is a collection of fixed size with random access (i.e. you can index it).

The list is a collection with a variable size (i.e. you can add and remove items) with random access.

IList is an interface that abstracts the functionality of a list (number, add, delete, indexer access) from various specific classes, such as List, BindingList, ObservableCollection, etc.

+80
Apr 19 '09 at 3:03
source share
— -

IEnumerable is an interface that allows iteration through a set of elements (for example, through the foreach keyword).

The array is inline .NET. It contains elements of the same type but has a fixed size. When you create an array with x elements, it cannot grow or shrink.

IList defines an interface for a list, and also implements IEnumerable.

The list implements the IList interface; This is a specific type of list.

The difference between .NET lists and arrays is that elements that they grow to be large enough to contain all the necessary elements can be added to the lists. The list stores this internally in the array and when the array is no longer large enough to hold all the elements, a new array is created and the elements copied to.

IList and arrays implement IEnumerable. The way interfaces work - classes implement the contract and behave in a similar way and can be considered similarly as a result (you know that the class implements IEnumerable, you do not need to know how or whys). I suggest you familiarize yourself with interfaces, etc.

+13
Apr 19 '09 at 3:03
source share

IEnumerable and IList interfaces . Array and list are classes. The array implements IEnumerable. The list implements IList, which extends IEnumerable.

Edit: as mentioned in itowlson comments, Array also implements IList.

+9
Apr 19 '09 at 3:00
source share

Generating an IEnumerable collection is lazy. Example:

 public IEnumerable<int> GetTwoInts() { yield return 1; yield return 2; } public void Something() { var twoInts = GetTwoInts(); } 

In the Something method, calling GetTwoInts () will not actually trigger the GetTwoInts method, since the enumeration never repeats.

+6
Nov 16 '09 at 3:40
source share

IEnumerable is a generic interface that is used by many classes, such as Array , List and String , to allow someone to iterate over a collection. Basically, this is what drives the foreach .

IList , as a rule, expose variables of type List end users. This interface allows random access to the main collection.

+2
Apr 19 '09 at 3:05
source share

This is an old post, but he was still thinking of answering. IEnumerable is behavior, while Array is a data structure (a continuous set of elements with a fixed size, making it easy to access elements by index). When Array implements IEnumerable, it should also display the ennumerable inherent property (simplifying iteration over the collection).

0
Aug 25 '15 at 17:25
source share

In addition to the other answers, note that when executing the foreach statement, there is a performance difference between IList<T> and List<T> .

This is because the iterator returned by List<T>.GetEnumerator is a type value, whereas the returned IList<T>.GetEnumerator is a reference type and therefore requires memory allocation (see List Type Value Enumerator in C # ) .

In my opinion, IList<T> not a very good interface. For example, a call to Add may call (see Why does an array implement IList? ). If you need encapsulation, you'd better use IEnumerable<T> or IReadOnlyList<T> .

0
Nov 07 '17 at 16:01
source share



All Articles