Can you help me understand the practical differences between these two;
IList<int> myList = new List<int>();
List<int> myList = new List<int>();
Can you help me understand the practical differences between these two;
IList<int> myList = new List<int>();
List<int> myList = new List<int>();
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:
The real difference is easier to see when you do something like this:
IList<int> myList;
if (x == true)
myList = getBindngList();
else
myList = getList();
public BindingList<int> getBindingList()
{
// do important things...
return new BindingList<int>();
}
public List<int> getList()
{
// do important things...
return new List<int>();
}
Because both List and BindingList implement the IList interface, you can access them through a variable typed as IList. If your variable was a List, and the method that returned the data you wanted returned a BindingList, well you'd have to take the hard road and add all of the members of the returned BindingList to a new List or just write another method.
Basically, IList<>
can by implemented by any class (one of which is List<>
). Therefore, using IList<>
for parameters and properties makes the coupling more loose, which is usually a good thing, since you are not restricted to one specific implementation.
If you use the second form, you'll be able to call methods on myList that are part of the List class, but not part of the IList interface without a cast.
If you use the first form, you'd be able to assign other instances of IList to myList in case you wanted to use a different IList implementation later.
When deciding which one to use, the best practice is generally to use the most generic form possible that has all the functionality you need. If IList has all the methods you need, then use that. If IEnumerable has everything you need, then you could be even more generic and use that instead.
IList won't get you all the functions you get with List.
IList is an interface and is implemented by many list - like classes - one of those classes ist List.
I will give you a very simple example
Lets say we have a class library and we have class1
as class
It has 2 methods
public IList getList()
{
IList mylist=new List();
mylist.add("hello");
mylist.add("hi");
return mylist;
}
public List getList()
{
List mylist=new List();
mylist.add("hello");
mylist.add("hi");
return mylist;
}
Now when the class library is being used. and it happens than you need to change the Collection from List to Array or indexed type.
We can do this like
public IList getList()
{
IList mylist=new int[10];
lst[0] = 10;
return mylist;
}
But We cannot do this
public List getList()
{
List mylist=new int[10];
lst[0] = 10;
return mylist;
}
Your first example will only be usable as an IList<int>
without "casting" using the as
syntax, whereas your second example will also be usable as an IEnumerable<int>
and ICollection<int>
right away.