35

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

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

List<int> myList = new List<int>();
kaivalya
  • 3,829
  • 9
  • 34
  • 43
  • 6
    But in C# there is a difference between `string s = "x"` and `object o = "x"`. See all the responses below... – Tomas Aschan Sep 02 '09 at 13:00
  • 1
    http://stackoverflow.com/questions/764748/whats-the-difference-between-ienumerable-and-array-ilist-and-list – Mehrdad Afshari Sep 02 '09 at 13:01
  • 3
    @Tomas: My point was: this is just a special case of a fundamental difference in an object oriented language. You shouldn't look at it as a special case. You should grasp the concept of interfaces in general to see why. – Mehrdad Afshari Sep 02 '09 at 13:03

7 Answers7

47
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

Samuel Caillerie
  • 8,259
  • 1
  • 27
  • 33
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
15

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.

apiguy
  • 5,282
  • 1
  • 23
  • 24
5

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.

Lucero
  • 59,176
  • 9
  • 122
  • 152
2

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.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
1

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.

bernhardrusch
  • 11,670
  • 12
  • 48
  • 59
0

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;
}
Marko
  • 20,385
  • 13
  • 48
  • 64
mahendra
  • 9
  • 1
-1

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.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • 2
    Wrong. IList<> inherits from ICollection<>, which inherits from IEnumerable<>. Therefore, those are also "available right away". – Lucero Sep 02 '09 at 13:02
  • 2
    Casting (no quotes needed), doesn't have to be done with the `as` keyword. In fact, something like `(list as List).Sort()` is particularly bad, because you're trading a possible InvalidCastException, which tells what the problem is, for a NullReferenceException, which is misleading. – P Daddy Sep 02 '09 at 13:11