What is difference between ArrayList and Hashtable in C#?



An ArrayList is a dynamic array that grows as new items are added that go beyond the current capacity of the list. Items in ArrayList are accessed by index, much like an array.
The Hashtable is a hashtable behind the scenes. The underlying data structure is typically an array but instead of accessing via an index, you access via a key field which maps to a location in the hashtable by calling the key object's GetHashCode() method.
In general, ArrayList and Hashtable are discouraged in .NET 2.0 and above in favor of List<T> and Dictionary<TKey, TValue> which are much better generic versions that perform better and don't have boxing costs for value types.

A hashtable will map string values to values in your hashtable. An arraylist puts a bunch of items in numbered order.
Example:

Hastable ht = new Hastable();
ht("examplenum") = 5;
ht("examplenum2") = 7;

//Then to retrieve
int i = ht("example");  //value of 5
ArrayList al = new ArrayList();

al.Add(2);
al.Add(3);

//Then to retrieve
int j = al[0]  //value of 2




Post a Comment

0 Comments