views:

2665

answers:

9

I've seen them used in a lot of the same ways, and I am worried I'm about to go down a path in design that is irreversible if I don't understand this better. Also, I am using .NET.

A: 

Both implement the same interfaces, so they'll behave the same way. Perhaps they are implemented differently internally, but this would have to be tested.

The only real differences I see are the namespaces and the fact that Collection<T> is marked with ComVisibleAttribute(false), so COM code can't use it.

OwenP
They implement different interfaces - List<T> implements IList<T>, where Collection<T> does not.
Bevan
+3  A: 

List is faster.

Do for example

private void button1_Click(object sender, EventArgs e)
{
  Collection<long> c = new Collection<long>();
  Stopwatch s = new Stopwatch();
  s.Start();
  for (long i = 0; i <= 10000000; i++)
  {
    c.Add(i);
  }
  s.Stop();
  MessageBox.Show("collect " + s.ElapsedMilliseconds.ToString());

  List<long> l = new List<long>();
  Stopwatch s2 = new Stopwatch();
  s2.Start();
  for (long i = 0; i <= 10000000; i++)
  {
    l.Add(i);
  }
  s2.Stop();
  MessageBox.Show("lis " + s2.ElapsedMilliseconds.ToString());


}

on my machine List<> is almost twice as fast.

Edit

I can't understand why people are downvoting this. Both on my work machine and my home machine the List<> code is 80% faster.

tuinstoel
How is it faster? Lookup? Insertion? Removal? Search? Why is it faster?
Doug T.
List is fewer letters to type than Collection :)
RedFilter
Faster? Prove it.
ctacke
Collection has fewer methods. Therefore it's faster. QED. (joking, I'm not trolling)
Ray
Tried it on my machine and list is about 20% faster. Would be interested in some discussion on why this might be. Maybe the list is better with allocating memory.
Ray
On my machine list is 30% faster.
nightcoder
List's methods aren't inheritable so there aren't checks to see if they have been inherited; Collection's methods are inheritable. The advantage is that you can use Collection as a base class to inherit from and create a custom Collection.
Richard Gadsden
+4  A: 

List represents a collection where the order of items is important. It also supports methods s.a. Sort and search. Collection is a more general data-structure which makes less assumptions about the data and also supports less methods to manipulate it. If you want to expose a custom data structure, you should probably extend the collection. If you need to manipulate data w/o exposing the data-structure, a list is probably the more convenient way to go.

Manu
+1  A: 

This is one of those grad school questions. A Collection of T is sort of abstract; there may be a default implementation (I'm not a .net/c# guy) but a collection will have basic operations like add, remove, iterate, and so on.

List of T implies some specifics about these operations: add should take constant time, remove should take time proportional to the number of elements, getfirst should be consant time. In general, a List is a kind of Collection, but a Collection isn't necessarily a kind of List.

Charlie Martin
+13  A: 

List<T> is intended for internal use within the application code. You should avoid writing public APIs that accept or return List<T> (consider using a superclass or a collection interface instead).

Collection<T> serves a base class for custom collections (although it can be used directly).

Consider using Collection<T> in your code unless there are specific features of List<T> that you need.

The above are just recommendations.

[Adapted from: Framework Design Guidelines, Second Edition]

Arnold Zokas
Where did these guidelines come from?
Terry Donaghe
Sorry, I should've mentioned this.Framework Design Guidelines, Second Release
Arnold Zokas
It looks like Collection<T> has been removed from System.Collections.Generic as of .NET 3.5, so there might be migration issues if you used Collection<T> instead of List<T>?
Terry Donaghe
I believe that Collection<T> is defined in the System.Collections.ObjectModel namespace [assembly:mscorlib]
Arnold Zokas
Ahh! Thanks ArnieZ!
Terry Donaghe
+2  A: 

Hanselman Speaks: "Collection<T> looks like a list, and it even has a List<T> internally. EVERY single method delegates to the internal List<T>. It includes a protected property that exposes the List<T>."

EDIT: Collection<T> doesn't exist in System.Generic.Collections .NET 3.5. If you migrate from .NET 2.0 to 3.5 you'll need to change some code if you're using a lot of Collection<T> objects, unless I'm missing something obvious...

EDIT 2: Collection<T> is now in the System.Collections.ObjectModel namespace in .NET 3.5. The help file says this:

"The System.Collections.ObjectModel namespace contains classes that can be used as collections in the object model of a reusable library. Use these classes when properties or methods return collections."

Terry Donaghe
+12  A: 

Collection<T> is a customizable wrapper around IList<T>. While IList<T> is not sealed, it doesn't provide any customization points. Collection<T>'s methods are by default delegated to the standard IList<T> methods, but can be easily overridden to do what you want. It is also possible to wireup events inside a Collection<T> that I don't believe could be done with an IList.

In short, it's much easier to extend it after the fact, which could potentially mean a lot less refactoring.

Adam Lassek
Well, around IList<T> (List<T> is just the default), but good answer other than that.
Marc Gravell
Good point. Corrected.
Adam Lassek
+10  A: 

List<T> is a very commonly seen container, because it is so very versatile (with lots of handy methods like Sort, Find, etc) - but has no extension points if you want to override any of the behaviour (check items on insert, for example).

Collection<T> is a wrapper around any IList<T> (defaulting to List<T>) - it has the extension points (virtual methods), but not as many support methods like Find. Because of the indirection, it is slightly slower than List<T>, but not by much.

With LINQ, the extra methods in List<T> become less important, since LINQ-to-Objects tends to provide them anyway... for example First(pred), OrderBy(...), etc.

Marc Gravell
Collection<T> lacks method foreach, even in Linq-to-Objects.
tuinstoel
@tuinstoel - but it is trivial to add.
Marc Gravell
A: 

I really like the way this post answers the question: http://blogs.msdn.com/fxcop/archive/2006/04/27/585476.aspx