Monday, 20 November 2006

.net collection classes summary

There are many collection classes provided in .net framework. To use these classes appropriately is important for a developer. And it's quite essential to understand the subtle difference between each class.

These classes are summarized below.

1. ArrayList. This is the most common class used in .net 1.1. It's a resizeable, index-based collection of objects. Some important methods include Add, AddRange, Remove, Contains and IndexOf. This class is replaced by generic List<> class in .net 2.0. To interate over items in an ArrayList, we can use IEnumerator interface. eg.
IEnumerator en = ar.GetEnumerator();
while(en.MoveNext())
{
string s = en.Current as string;
...
}
ArrayList provides a Sort method. The sort method uses the default implementation of IComparer - Comparer class to do the comparison.

2. Queue class. This is a collection of objects with first-in, first-out feature. The Dequeue method read and remove the item from the front of the queue. The Enqueue method adds an item to the end of the queue. And Peek method only read the first item in the queue.

3. Stack class. The Stack class is a last-in, first-out collections of object. The Pop method reads and removes an ite mfrom the top of the stack. The Push method adds an item on the top of the stack. And the Peek method reads the last item without removing it.

4. Hashtable class. This is a dictionary class, providing a key/value pair of lookup table. eg.
Hashtable ht = new Hashtable();
ht["Key"] = "Value";
which is equivalent to :
ht.Add("Key", "Value")
If you try to Add a same key twice, the class will throw an exception, but you can assign a same key as many times as you like, only the last value is kept.
One important feature of Hashtable is that wecannot access item by index number.

5. SortedList class. This class sort items when adding them to the list, and items can be accessed by their index. The use of this class is quite similar to Hashtable.

6. ListDictionary class. This class is lightweight and efficient class for a small number of items. The usage is identical to Hashtable.

7. HybridDictionary class. HybridDictionary is a dynamic collection. When the number of items are small, it implement itself as ListDictionary. Whe number of items becomes two large, it turns itself to Hashtable.

8. OrderedDictionary class. This class provides the same functionalities of Hashtable, as well as the ability of access items by their index number.

9. BitArray. This class holds an array of boolean values. eg.
BitArray ba = new BitArray(2);
ba[0] = false; ba[1] = true;

10. BitVector32 structure. The BitVector32 structure is very useful to maintain individual stutus in a single integer. Its useful in WebService when network bandwidth is limited. With one integer value, it can contain 32 true or false status.

11. StringCollection class. A simple collection only stores strings.

12. StringDictionary class. Similar to Hashtable, except both key and value are strings.

13. CollectionUtil class can create Hashtable and SortedList that are case insensitive. eg.
CollectionUtil.CreateCaseInsensitiveHashtable();

14. NameValueCollection class is similar to StringDictionary class. The difference of this class is it allows multiple values share a same key. And values can be retrieved by index as well as key. eg.

NameValueCollection col = new NameValueCollection ();
col.Add("key", "Value 1");
col.Add("key", "Value 2"); // then we have two values with same key.

If you add two values with same indexer, only the last value is kept. eg.]
col["key"] = "Value1";
col["key"] = "Value2"; // only Value2 is kept.


15. Generic collection classes. Almost for each class mentioned above there is a generic version. The use of these generic classes are similar. The only difference of Dictionary classes is the way of retriving items. The generic class KeyValuePair is used to read each item. eg.
SortedList list = new SortedList();
list["one"] = 1;
list["two"] = 2;
foreach(KeyValuePair i in list)
{
string s = i.Key;
int value = i.Value;
}

No comments: