Can you help me understand the practical differences between these two;
IList<int> myList = new List<int>(); List<int> myList = new List<int>();
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.
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.
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.
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.
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
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