locked
Difference between IList<> and List<>???

    Question

  • Hi,
       Can somebody explain what is difference between IList <> and List with an example??? N in which situation should i use IList and List????

    Thanks
    Saturday, August 01, 2009 6:08 PM

Answers

  • You missed the Big One, IEnumerable<>.  It is a consideration you make when you specify the argument for a public method.  What does your method need to do with the passed argument?  Does it simply need to enumerate the members of the collection with foreach?  Use IEnumerable and open up your method to be usable with lots of collection classes and Linq primitives.

    Does your method need to add, remove, clear elements from the collection?  Something IEnumerable<> cannot do.  Use IList<>.

    Does your method need to use a method that's only available in List<>?  List it is.

    Long story short: use the minimum to make it most usable.

    Hans Passant.
    • Marked as answer by Harry Zhu Friday, August 07, 2009 2:05 AM
    Sunday, August 02, 2009 12:17 AM
    Moderator
  • Hi,

    Just expand on nobugz post.

    List<T> is a concrete list, you can add T's to it.

    IList<T> is an object with list like behaviour.

    Can't think of a good example off hand but lets say List<T> and SortedList<T> they both have list like behavior so the both implement IList<T> but how they have been coded is very different the Add method for example does something a little different, List<T>.Add just adds the item, SortedList<T> finds the location to add the item and then inserts it.

    IList lets List and SortedList act the same way even though they do slightly different things.


    www.dsmyth.net | www.dsmyth.net/wiki
    • Marked as answer by Harry Zhu Friday, August 07, 2009 2:05 AM
    Sunday, August 02, 2009 3:10 PM

All replies

  • You missed the Big One, IEnumerable<>.  It is a consideration you make when you specify the argument for a public method.  What does your method need to do with the passed argument?  Does it simply need to enumerate the members of the collection with foreach?  Use IEnumerable and open up your method to be usable with lots of collection classes and Linq primitives.

    Does your method need to add, remove, clear elements from the collection?  Something IEnumerable<> cannot do.  Use IList<>.

    Does your method need to use a method that's only available in List<>?  List it is.

    Long story short: use the minimum to make it most usable.

    Hans Passant.
    • Marked as answer by Harry Zhu Friday, August 07, 2009 2:05 AM
    Sunday, August 02, 2009 12:17 AM
    Moderator
  • Hi,

    Just expand on nobugz post.

    List<T> is a concrete list, you can add T's to it.

    IList<T> is an object with list like behaviour.

    Can't think of a good example off hand but lets say List<T> and SortedList<T> they both have list like behavior so the both implement IList<T> but how they have been coded is very different the Add method for example does something a little different, List<T>.Add just adds the item, SortedList<T> finds the location to add the item and then inserts it.

    IList lets List and SortedList act the same way even though they do slightly different things.


    www.dsmyth.net | www.dsmyth.net/wiki
    • Marked as answer by Harry Zhu Friday, August 07, 2009 2:05 AM
    Sunday, August 02, 2009 3:10 PM