Definition of List<T>
in .net shows it implement various interfaces.
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
What changes interfaces with and without T
brings in IList
i.e. if one of my class implement IList<T>
and not IList
, then can I use it as my custom collection class or not?
In C# IList interface is an interface that belongs to the collection module where we can access each element by index. Or we can say that it is a collection of objects that are used to access each element individually with the help of an index. It is of both generic and non-generic types.
List and IList are used to denote a set of objects. They can store objects of integers, strings, etc. There are methods to insert, remove elements, search and sort elements of a List or IList. The major difference between List and IList is that List is a concrete class and IList is an interface.
The main difference between List and IList in C#.NET 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. List and IList are used to denote a set of objects.
Like IEnumerable, IList is also best to query data from in-memory collections like List, Array etc. IList is useful when you want to Add or remove items from the list. IList can find out the no of elements in the collection without iterating the collection. IList supports deferred execution.
The reason that List<T>
implements both IList<T>
and IList
is to make it usable anywhere your code is assuming an IList
. This will make it more easier to make a transition to the generic IList<T>
since it is the more appropriate one. Also, if you have a .net 1.1 or earlier code that you would like to reuse even though your class is implemented in a .net 2.0 or later assembly it will make it possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With