Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would you not use Generic Collections?

Tags:

c#

.net

generics

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?

like image 420
1800 INFORMATION Avatar asked Mar 31 '09 09:03

1800 INFORMATION


People also ask

Which is not a generic collection?

The following are the non-generic collections: ArrayList, BitArray. ArrayList − It represents ordered collection of an object that can be indexed individually. ArrayList is an alternative to an array.

Which of the following problems of non-generic collection does generics solve?

Code reusability: Generics help in reusing the code already written, thereby making it usable for other types (for a method, or class, or an interface).

Are generic collections type-safe?

Generic classes are used to provide the type safe structure -- type safe means only one type of data can be contained within Generic classes. The data type is defined at the time of an object initialization. We can have a different type of collection.

What is the use of generic collections?

The generic collections are introduced in Java 5 Version. The generic collections disable the type-casting and there is no use of type-casting when it is used in generics. The generic collections are type-safe and checked at compile-time. These generic collections allow the datatypes to pass as parameters to classes.


3 Answers

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.

like image 98
Jon Skeet Avatar answered Oct 04 '22 18:10

Jon Skeet


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.

like image 45
Richard Avatar answered Oct 04 '22 20:10

Richard


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.

like image 45
munificent Avatar answered Oct 04 '22 18:10

munificent