Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between List and IList [duplicate]

Tags:

c#

.net

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?

like image 755
Maxymus Avatar asked Mar 16 '11 08:03

Maxymus


People also ask

What is the difference between IList and List?

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.

Should I use List or IList?

Microsoft guidelines as checked by FxCop discourage use of List<T> in public APIs - prefer IList<T>.

Is IList faster than List?

Results. IList<T> uses 40 Bytes more than List<T> .

What is 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.


2 Answers

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.

like image 65
Hassan Gulzar Avatar answered Sep 23 '22 16:09

Hassan Gulzar


List implements IList interface

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

like image 21
Arsen Mkrtchyan Avatar answered Sep 23 '22 16:09

Arsen Mkrtchyan