33

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

What's the difference between the above two?

Community
  • 1
  • 1
user496949
  • 79,431
  • 144
  • 301
  • 419

5 Answers5

75

A List<string> is a concrete implementation of IEnumerable<string>. The difference is that IEnumerable<string> is merely a sequence of string but a List<string> is indexable by an int index, can be added to and removed from and have items inserted at a particular index.

Basically, the interface IEnumerable<string> lets you stream the string in the sequence but List<string> lets you do this as well as modify and access the items in the lists in specific ways. An IEnumerable<string> is general sequence of string that can be iterated but doesn't allow random access. A List<string> is a specific random-access variable-size collection.

jason
  • 228,647
  • 33
  • 413
  • 517
  • 5
    This answer was more helpful than those at http://stackoverflow.com/questions/764748/whats-the-difference-between-ienumerable-and-array-ilist-and-list – JYelton Apr 11 '11 at 16:43
  • What does a concrete implementation mean? Does it mean a list inherits from ienumerable? – user1534664 Apr 21 '13 at 12:20
  • 1
    @user1534664: No. It means it inherits from `IEnumerable` *and* it's not abstract, it's actually constructible. – jason Aug 09 '13 at 00:13
11

different.

IEnumerable enables you to iterate through the collection using a for-each loop.

And IEnumerable just have method GetEnumerator.

And List it implement many interface like IEnumerable, Ilist, etc. So many function in List.

In performance IEnumerable faster than List.

aeruL
  • 375
  • 2
  • 6
  • 16
4

IEnumerable<T> is an interface. It must be implemented.

List<T> is one implementation of IEnumerable<T>

Robert Harvey
  • 173,679
  • 45
  • 326
  • 490
2

One is an interface: http://msdn.microsoft.com/en-us/library/9eekhta0.aspx

The other is a class that implements that interface: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Also, List is an array that grows when you add elements to it, while IEnumerable allows implementers to be used in a foreach.

Etienne de Martel
  • 32,266
  • 8
  • 91
  • 105
2

The first is a concrete List of strings, the other is any class implementing IEnumerable<string>

Phil
  • 141,914
  • 21
  • 225
  • 223