Tuesday, March 16, 2010

Difference between ArrayList And HashTable And Dictionary

I was always keen to know the difference between ArrayList,HashTable & Dictionary. Here it goes.

An ArrayList behaves just like an array, except that it will dynamically expand and contract when you add/remove elements to it. A HashTable is useful when you want to lookup objects indexed by a 'key' value. It also dynamically expands and contracts but doesn't impose an order on the elements like an array does. Put a different way: you index elements in an ArrayList with a numeric index, you index elements in a HashTable with a key. Such a key is often a string.

A Dictionary is a hash table.Dictionary is a generic type, Hashtable is not. That means you get type safety with Dictionary, because you can't insert any random object into it, and you don't have to cast the values you take out.

Thanks to the following links.

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/84b8d2a1-e84d-446a-94bb-4ec54d3b38cf

http://stackoverflow.com/questions/301371/why-dictionary-is-preferred-over-hashtable-in-c

1 comment: