67

Can anyone tell me when I should use either.

For example, I think I should use an IList when I want to access the .Count of the collection or an individual item, correct?

Thank you.

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254

3 Answers3

123

Generally speaking, you should try and use the least specific type that suits your purpose. IEnumerable is less specific than IList (IList implements IEnumerable) so unless you want something specific from IList (such as Count as you suggest, or perhaps Add, Delete, etc), I'd use IEnumerable.

One benefit of remaining with IEnumerable is that you can write iterator methods to return this type (look up "yield return" and iterator methods if you are not familiar with them). This allows you to write very memory efficient "pipelines" for your loops.

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
  • 3
    I agree with using the least derived type. ICollection<> is sufficient for most adding/removing. – Mark H Jul 12 '10 at 13:33
  • @Mark H - good point - for most add/remove operations, even IList is unnecessarily specific. – Rob Levine Jul 12 '10 at 13:45
  • IList *implements* IEnumerable?! – Yehuda Shapira Apr 08 '13 at 14:01
  • 1
    Yes: IList does implement IEnumerble: http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx. After all, a list is an enumerable collection of things that can be iterated over. – Rob Levine Apr 10 '13 at 07:36
  • 1
    I think ultimately, it really depends on what set of functions/methods you want to provide to your class consumers. To say that we should try to use the least specific type is at least not accurate. People will ask: Why don't we use object all the time then since object is the least derived type? – Stack0verflow Mar 18 '16 at 19:23
  • 15
    You've misunderstood the point I think. I'm not suggesting you always use the least derived type "full-stop". You use the least specific type *that suits your purpose* and has the methods and properties you need. So, for example, system.object would be a good choice if all you were doing was accessing the type on the object via GetType, but it is no good if you expect it to be a list of items. If you expect a list of items but only want to enumerate them, IEnumerable would be a better choice, but no good if you wanted to add to the list, in which case IList is the better choice. – Rob Levine Mar 19 '16 at 12:38
  • One advantage of IEnumerable is that it is not modifiable (if you want to adopt more functional style of programming). – Endy Tjahjono Jan 15 '19 at 09:46
42

You use IEnumerable when you want to loop through the items in a collection.

IList is when you want to add, remove, and access the list contents out of order.

IList vs IEnumerable for Collections on Entities

http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=722

Community
  • 1
  • 1
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • 2
    I have an issue with this answer. You want to use IList when iterating over a collection multiple times. Otherwise you end up with multiple unnecessary enumerations – JSON Apr 20 '21 at 15:09
15

You should use IList when you need access by index to your collection, add and delete elements, etc., and IEnumerable when you need just enumerate over your collection.

A very simple answer, I can extend it if you will describe you scenario.

Restuta
  • 5,855
  • 33
  • 44