[.net] [C#] ArrayList/List?

Started by
3 comments, last by joelmartinez 17 years, 4 months ago
I'm looking for something similiar to a C++ std::vector. I know in the past, I've used ArrayList. What's the difference between an ArrayList and a List? Are there any other containers I should know about?
Advertisement
ArrayList existed before generics allowed for properly 'templated' containers.
HashTable -> Dictionary<K,V> is the same thing.

To my knowledge, there is no benefit to continued use of the untyped collections.
For the most part the generic template containers are better than the untyped containers.

Here are the generics that you should know about:

List
Dictionary
Stack

Here are a few more:

LinkedList
SortedDictionary
Queue

Generic containers basically make it easier for the compiler to provide better code, and they make it easier for you not needing to type in castes and write wrappers all the time.

Don't dismiss the use of the old containers entirely though. They still have utility in some scenarios.
Generics avoid the need for boxing/unboxing, which costs, so unless you're using a collection of different types in the same list there's no need to not use generics, however, I'm not certain how derived objects are handled, whether they're boxed or not, so if you have a collection based on a shared base class whether this has as much penalty as just using a none generic collection I don't know.
Quote:Original post by Niksan
Generics avoid the need for boxing/unboxing, which costs, so unless you're using a collection of different types in the same list there's no need to not use generics, however, I'm not certain how derived objects are handled, whether they're boxed or not, so if you have a collection based on a shared base class whether this has as much penalty as just using a none generic collection I don't know.
Boxing only happens when you're storing a value type as an object. Using List<object> is essentially the same as using ArrayList from a boxing perspective.
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]

This topic is closed to new replies.

Advertisement