Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between IEnumerable and Array, IList and List?

Tags:

c#

What's the difference between IEnumerable and Array?

What's the difference between IList and List?

These seem to have the same function.

like image 460
chat Avatar asked Apr 19 '09 02:04

chat


People also ask

What is the difference between IList and IEnumerable?

IList doesn't support further filtering. IEnumerable exists in System. Collections Namespace. IEnumerable is a forward only collection, it can't move backward and between the items.

Is it better to use IEnumerable or List?

IEnumerable is more efficient and faster when you only need to enumerate the data once. The List is more efficient when you need to enumerate the data multiple times because it already has all of it in memory.

What is difference between IList and List in C#?

The main difference between List and IList in C# is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index.

Which is faster IEnumerable or List?

1) if you iterate elements from IEnumerable or List only one time, so both are same, no differences. 2) if you iterate elements many times, or if you get an one element for multiple times, so, IEnumerable will be slow.


2 Answers

IEnumerable provides only minimal "iterable" functionality. You can traverse the sequence, but that's about it. This has disadvantages -- for example, it is very inefficient to count elements using IEnumerable, or to get the nth element -- but it has advantages too -- for example, an IEnumerable could be an endless sequence, like the sequence of primes.

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

List is a variable-size collection (i.e. you can add and remove elements) with random access.

IList is an interface which abstracts list functionality (count, add, remove, indexer access) away from the various concrete classes such as List, BindingList, ObservableCollection, etc.

like image 171
itowlson Avatar answered Sep 30 '22 18:09

itowlson


IEnumerable is an interface that allows the iteration through a collection of items (e.g. via the foreach keyword).

An array is a .NET intrinsic. It holds items of the same type, but it is of a fixed size. Once you create an array with x elements, it cannot grow or shrink.

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

List implements the IList interface; it is a concrete type of list.

The difference between .NET Lists and arrays is that lists can have elements added to them -- they grow to be big enough to hold all of the required items. The list stores this internally in an array and, when the array is no longer big enough to hold all of the elements, a new array is created and the items copied across.

IList & arrays both implement IEnumerable. That's how interfaces work -- classes implement the contract and behave in a similar fashion and can be treated similarly as a result (you know that the class implements IEnumerable, you don't need to know the hows or the whys). I suggest you read up on interfaces and so forth.

like image 31
Mark Simpson Avatar answered Sep 30 '22 18:09

Mark Simpson