What is the difference between IList and IList <T>

The definition of List<T> in .net shows that it implements various interfaces.

 public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable 

What changes the interfaces with and without T , IList contributes, that is, if one of my classes implements IList<T> , and not IList , can I use it as my own collection class or not?

+4
source share
2 answers

The reason List<T> implements both IList<T> and IList should make it usable wherever your code accepts IList . This will simplify the transition to the generic IList<T> , since it is more suitable. Also, if you have .net 1.1 or earlier code that you want to reuse, even if your class is implemented in the .NET version or later, this will be possible.

+10
source

In IList<T> you can only put a specific type of object (T), and IList can contain different types of objects

This code below will not match because AdressInformation is not a valid object in the client list

 IList<Customer> customers = new List<Customer>(); customers.Add(new Customer()); customers.Add(new AdressInformation()); 

This code will compile, but an exception will be thrown at runtime

 IList customers = new List<Customer>(); customers.Add(new Customer()); customers.Add(new AdressInformation()); 
+5
source

All Articles