Sunday, March 20, 2011

when to use Collection<T> vs List<T>

What is the best practice for when to use one vs the other?

Both implement IList<T> and hold the data in an ordered fashion, but only List expose the sorting semantics....

(edit: duplicate from here)

From stackoverflow
  • Well, based on what you say, I think you should use collection when you just need to store the data and you don't care at all in what order.

    Marc Gravell : That applies equally to either - just don't call Sort; there are much bigger differences - see the details in the "duplicate from here" thread)
  • in this question you can see the difference between list and collection of T

  • Scott Hanselman asked this question once. You can read his related blog post here.

  • Collection<T>:

    Provides the base class for a generic collection.

    List<T>:

    Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

    So, according the docs, one is intended as a base class for collections. The other is intended for use as a container.

    So use the List and inherit from the Collection.

0 comments:

Post a Comment