Sign in to follow this  
Uphoreum

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this