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.