9

Possible Duplicate:
C# - List<T> or IList<T>

I have a class

 public class Employee
 {
      public int Id { get; set; }
      public string Name { get; set; }
 }

And I need to define a list and what is the difference between defining it in below ways

IList<Employee> EmpList ;

Or

List<Employee> EmpList ;
Community
  • 1
  • 1
Coder
  • 307
  • 1
  • 3
  • 10

9 Answers9

14

IList<> is an interface. List<> is a concrete class.

Any of these will be valid:

 IList<Employee> EmpList = new List<Employee>();

and

 List<Employee> EmpList = new List<Employee>();

or

 var EmpList = new List<Employee>(); // EmpList is List<Employee>

However, you cannot instantiate an interface, i.e. the following will fail:

IList<Employee> EmpList = new IList<Employee>();

In general, classes and methods which use dependencies (such as collections) should specify the least restrictive interface possible (i.e. the most general one). e.g. if your method just needs to iterate a collection, then an IEnumerable<> will suffice:

public void IterateEmployees(IEnumerable<Employee> employees)
{
   foreach(var employee in employees)
   {
     // ...
   }
}

Whereas if a consumer needs to access the Count property (as opposed to having to iterate the collection via Count()), then a ICollection<T> or better, IReadOnlyCollection<T> would be more appropriate, and similarly, IList<T> would only be required when needing random access to the collection via [] or to express that new items need to be added or removed from the collection.

user11336341
  • 95
  • 1
  • 3
StuartLC
  • 104,537
  • 17
  • 209
  • 285
7

IList<T> is an interface implemented by List<T>.

You cannot create a concrete instance of an interface so:

//this will not compile
IList<Employee> EmpList = new IList<Employee>();    

//this is what you're really looking for:
List<Employee> EmpList = new List<Employee>();

//but this will also compile:
IList<Employee> EmpList = new List<Employee>();
pdriegen
  • 2,019
  • 1
  • 13
  • 19
  • 2
    yes i agree but i want to know depending on what situation we need to decide which to use: List EmpList = new List(); IList EmpList = new List(); – Coder Sep 11 '12 at 12:31
6

There are two answers here. For storing the actual list, use an List<T> because you need a concrete data structure. However, if you return it from a property or require it as an argument, consider a IList<T>. It is more generic, allowing more types to be passed it for the argument. Similarly, it allows more types to be returned than just the List<T> in case the internal implementation changes. Indeed, you may want to consider an IEnumerable<T> for the return type instead.

akton
  • 14,148
  • 3
  • 43
  • 47
  • At which point we need to use IList and List ? I know IList is an interface and List inherit this interface. Please see below example - public class Test – Coder Sep 11 '12 at 15:42
  • 1
    @user1521931 This is a design question. In general, I would recommend using `List` for your fields and `IList` for properties and arguments. – akton Sep 12 '12 at 01:16
2

I'll leave you to enumerate the differences, perhaps with some nifty reflection, but a List<T> implements several interfaces, and IList<T> is only one of them:

[SerializableAttribute]
public class List<T> : IList<T>, ICollection<T>, 
    IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, 
    IEnumerable
Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
2

List object allows you to create a list, add things to it, remove it, update it, index into it etc etc. List is used whenever you just want a generic List where you specify object type in it and that's it.

IList on the other hand is an Interface. (For more on interfaces see MSDN Interfaces). Basically, if you want to create your own type of List, say a list class called SimpleList, then you can use the Interface to give you basic methods and structure to your new class. IList is for when you want to create your own, special sub-class that implements List. You can see example here

Maddy
  • 263
  • 1
  • 9
  • 24
1

There are many types of Lists. Each of them inherits from an IList (which is why it's an interface). Two examples are the List (regular list), and a Paged List (this is a list that has support for paging - it is used commonly in paged search results). A Paged List and a List are both types of ILists, which means that an IList is not necessary a List (it can be a Paged List) or vice-versa.

See this link on a PagedList. https://github.com/TroyGoode/PagedList#readme

tehdoommarine
  • 1,868
  • 3
  • 18
  • 31
0

IList is an interface, List is a class that implements it, the List type explicitly implements the non generic IList interface

0

The first version is programming to interfaces and is preferred (assuming you only need to use the methods defined by IList). The second version, with its declaration based on a specific class, is unnecessarily rigid.

Clafou
  • 15,250
  • 7
  • 58
  • 89
0

The difference is that IList is an interface and List is a class. List implements IList, but IList can't be instantiated.

antonijn
  • 5,702
  • 2
  • 26
  • 33