61

Possible Duplicates:
IList<int> vs List<int>
C# - List<T> or IList<T>

What is the difference between List and IList, which one has better performance and when to use List over IList and vice versa?

Community
  • 1
  • 1
Maxymus
  • 1,460
  • 2
  • 14
  • 22

3 Answers3

17

In C# List is a concrete implementation of the IList interface. The List is an implementation of the IList Interface. The idea is to program against the interface, not the implementation. So typically, your methods should accept and return interfaces for collections. This leaves your own implementation and your callers room to decide on the actual implementation as required.

Benefit of using an Interface is that you get to implement your functionality or better yet, the only functionality you require. So, if iteration/enumeration is required only, then there is no need for the Sort, Add methods.

DoomerDGR8
  • 4,840
  • 6
  • 43
  • 91
  • IList has an Add() method, but not a Sort(). – Joseph Poirier May 07 '19 at 21:03
  • IList can be read only, in which the .Add will throw. E.g. array. If you use IList together with add, you need to check .IsReadOnly everytime and handle that. Else use List. You can't really argue "oh but I KNOW that I will always pass in a List here", then you should take a List not an IList, else you are breaking Liskov substitution principle: "if S is a subtype of T, then objects of type T may be replaced with objects of type S" – Peheje Aug 05 '20 at 06:55
5

List implements IList interface

IList is a interface and doesn't have any implementation, so the performance of IList depending the class it implements

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
2

IList is the interface - see this question for more information - List<T> or IList<T>

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165