Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between IList and IList<T>

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?

like image 997
user1888859 Avatar asked Dec 22 '12 12:12

user1888859


People also ask

What is IList <>?

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.

What does IList mean?

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.

Which is better IList or list?

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.

What is the difference between IList and IEnumerable in C#?

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.


1 Answers

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.

like image 88
Ikaso Avatar answered Sep 30 '22 16:09

Ikaso