1800 INFORMATION
1800 INFORMATION

Reputation: 135443

When would you not use Generic Collections?

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?

Upvotes: 5

Views: 3001

Answers (8)

supercat
supercat

Reputation: 81307

The non-generic Collection in the Microsoft.VisualBasic namespace has some annoying quirks and goofiness, and is in a lot of ways pretty horrible, but it also has a unique feature: it is the only collection which exhibits sensible semantics if it's modified during an enumeration; code which does something like delete all members of a Collection which meet a certain predicate may need to be significantly rewritten if some other collection type is used.

Upvotes: 0

munificent
munificent

Reputation: 12374

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.

Upvotes: 3

JaredPar
JaredPar

Reputation: 755467

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.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

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.

Upvotes: 2

Justin Bannister
Justin Bannister

Reputation: 618

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.

Upvotes: 2

Igor Brejc
Igor Brejc

Reputation: 19004

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

Upvotes: 1

Richard
Richard

Reputation: 109190

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.

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1503649

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.

Upvotes: 17

Related Questions