views:

311

answers:

4

After moving to .NET 2.0+ is there ever a reason to still use the systems.Collections namespace (besides maintaining legacy code)? Should the generics namespace always be used instead?

A: 

In some circumstances the generic containers perform better than the old ones. They should at least perform as well as the old ones in all circumstances. And they help to catch programming errors. It's a rare combination of a more helpful abstraction and better performance, so there isn't much of a reason to avoid them. Only if you are forced to by a crummy library you have to work with that was written before generics.

Daniel Earwicker
+11  A: 

For the most part, the generic collections will perform faster than the non-generic counterpart and give you the benefit of having a strongly-typed collection. Comparing the collections available in System.Collections and System.Collections.Generic, you get the following "migration":

    Non-Generic               Generic Equivalent
    ------------------------------------------------------------
    ArrayList                 List<T>
    BitArray                  N/A
    CaseInsensitiveComparer   N/A
    CollectionBase            Collection<T>
    Comparer                  Comparer<T>
    DictionaryBase            Dictionary<TKey,TValue>
    Hashtable                 Dictionary<TKey,TValue>
    Queue                     Queue<T>
    ReadOnlyCollectionBase    ReadOnlyCollection<T>
    SortedList                SortedList<TKey,TValue>
    Stack                     Stack<T>

    DictionaryEntry           KeyValuePair<TKey,TValue>

    ICollection               N/A (use IEnumerable<T> or anything that extends it)
    IComparer                 IComparer<T>
    IDictionary               IDictionary<TKey,TValue>
    IEnumerable               IEnumerable<T>
    IEnumerator               IEnumerator<T>
    IEqualityComparer         IEqualityComparer<T>
    IList                     IList<T>

ICollection is immutable (no members to change the contents of the collection) while ICollection<T> is mutable. This makes the interfaces similar in name only while ICollection and IEnumerable<T> differ by very little.

From this list, the only non-generic classes that don't have a generic counterpart are BitArray and CaseInsensitiveComparer.

Scott Dorman
KeyedCollection<TKey, TItem> is an appropriate alternative to DictionaryBase, depending on the data you were storing.
Anthony Mastrean
A: 

I saw an interview with Anders Hejlsberg from the c# team and he was asked if there was anything he regretted with the previous releases of .net. Not having generics in asp.net 1.0 was the first thing he mentioned. Not having it there meant they had to implement workarounds that would stick with the .net libraries and soon become legacy code.

I never use the System.Collections namespace and from his statement this seem to be the correct path.

TT
A: 

About the only bad thing I can think of when using Generics is variance, so for example, if you have a List<Person> and you want to pass it to a method that takes List<object> you can't because List<Person> cannot be cast to List<object> directly.

This problem is solved in .NET 4.0.

Cameron MacFarland