tags:

views:

465

answers:

7

The advantage of using generics is that it increases the type safety - you can only put in the correct type of thing, and you get out the correct type without requiring a cast. The only reason I can think of for not using generic collections is that you need to store some arbitrary data. Am I missing something? What other reasons are there to not use generics when dealing with collections?

+3  A: 

The obvious other reason is working with code (possibly legacy) that does not use generic collections.

You can see this happening in .NET itself. System.Windows.Form.Control.Controls is not generic, nor is System.Web.UI.Control.Controls.

Richard
+9  A: 

If you need to store arbitrary data, use List<object> (or whatever). Then it's absolutely clear that it's deliberately arbitrary.

Other than that, I wouldn't use the non-generic collections for anything. I have used IEnumerable and IList when I've been converting an object reference and didn't know the type to cast it to at compile-time - so non-generic interfaces are useful sometimes... but not the non-generic classes themselves.

Jon Skeet
+1  A: 

Here's one answer: The change from Hashtable to Dictionary.

Igor Brejc
+2  A: 

The main advantage is the is no boxing or unboxing penalty with generic collections of value types. This can be seen if you examine the il using ildasm.exe. The generic containers give better performance for value types and a smaller performance improvement for reference types.

Justin Bannister
+2  A: 

Type variance with generics can trip you up, but mostly you should use generic collections. There isn't a really a good reason to avoid them, and all the reason in the world to avoid un-typed collections like ArrayList.

Joel Coehoorn
Woah lots to read there, thanks for the great answer
1800 INFORMATION
+1  A: 

One thing I think you need to consider is that a generic collection is not always a drop in replacement for a non-generic collection. For example, Dictionary<object,object> can not simply be plugged in for an instance of Hashtable. They have very different behavior in a number of scenarios that can and will break programs. Switching between these two collections forces a good programmer to examine the use cases to ensure the differences do not bite them.

JaredPar
Can you give an example? I cant think of any reason why hashtable and dictionary<object,object> arent interchangable. (minus a few syntax changes obviously)
Jason Coyne
Actually the classic example is the behaviour when the item is not found. HashTable returns null while dictionary throws an exception
1800 INFORMATION
hrm. That is certainly true, but id still treat that as a syntax-ish issue. a simple extension method to return null if the key is not contained would get you back to hash functionality
Jason Coyne
+2  A: 

Generics are almost always the right thing to use. Note that languages like Haskell and ML essentially only allow that model: there is no default "object" or "void*" in those languages at all.

The only reasons I might not use generics are:

  1. When the appropriate type is simply not known at compile time. Things like deserializing objects, or instantiating objects through reflection.

  2. When the users that will be using my code aren't familiar with them (yet). Not all engineers are comfortable using them, especially in some more advanced patterns like the CRTP.

munificent