Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IList<int> vs List<int> [duplicate]

Tags:

c#

Can you help me understand the practical differences between these two;

IList<int> myList = new List<int>();  List<int> myList = new List<int>(); 
like image 503
kaivalya Avatar asked Sep 02 '09 12:09

kaivalya


People also ask

What is the difference between IList and List?

The main difference between List and IList in C# 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.

Should I return IList or List?

Generally best practice is to accept parameters of the most generic type and to return the most specific. However, conventionally programmers tend to not want to tie themselves to the List implementation and normally return the IList interface.

What is IList in C# with example?

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 is the difference between ICollection and IEnumerable?

IEnumerable has one property: Current, which returns the current element. ICollection implements IEnumerable and adds few additional properties the most use of which is Count. The generic version of ICollection implements the Add() and Remove() methods.


1 Answers

IList<int> myList = new List<int>(); 

will only expose the methods exposed by the interface IList

List<int> myList = new List<int>(); 

will expose all members of the List object

I'm not going to list all the differences here, but you can check out the member lists on MSDN for a complete list for each:

IList(T) Members
List(T) Members

like image 108
Justin Niessner Avatar answered Sep 25 '22 06:09

Justin Niessner