If you need a collection that stores data in a sequential basis, you can use a Queue which is a first-in, first-out (FIFO) or Stack which is a last-in, first-out (LIFO). Queue’s most important properties and methods are: Count, Dequeue(), Enqueue(), and Peek(). Stack’s most important properties and methods are: Count, Pop(), Push(), and Peek().
Dictionaries are another type of collections that are used to store key/value pairs. The most basic of all dictionary classes that are available in .NET Framework is the Hashtable class.
|Hashtable emailLookup = new Hashtable();
// using Add() method
// using indexer to add key/value pair
// using indexer to access data
// iterating through the entries in the dictionary
All dictionary classes support the IDictionary interface which derives from ICollection interface. IDictionary’s most important properties and methods are: Keys, Values, Add(), Clear(), Contains(), GetEnumerator(), and Remove(). Hashtable also has additional methods for testing existence of keys and values: ContainsKey() and ContainsValue().
Hashtable and Equality
The Hashtable uses a hash value, an integer, to aid in the storage of its keys. It uses the GetHash() method that the Object class supports to test for equality. The Object.GetHash() method returns a hash value that is unique to the object. For String class, this GetHash() method is overridden so that two strings with the same text will be equal even though they are different instances.
Care should be taken that when creating your own class that will be stored in a Hashtable that you override the GetHash() method if the test for equality is wrong. You also might need to override the Equals() method because if the Hashtable finds that the two objects have same hash value, it will call their Equals() method to see if they are in fact equal. The Object.Equals() method returns false if the two objects are different instances of the same class.
|public class Dog
public Dog(string name)
public override int GetHashCode()
public override bool Equals(object obj)
Another way to specify your own equality comparison is through the IEqualityComparer interface and this is useful if you do not want to change your classes to override the GetHash() and Equals() methods.
|public class InsensitiveComparer : IEqualityComparer
CaseInsensitiveComparer _comparer = new CaseInsensitiveComparer();
public int GetHashCode(object obj)
public new bool Equals(object x, object y)
// to use the InsensitiveComparer class