Page Stats
Visitor: 130
Dictionary
Dictionary is a collection that store items in a key-pair manner. Each value in the collection is identified by its key and will be unique, Similar to English dictionary where each word (Key) has its corresponding meaning (value). The two common types of Dictionary in the System.Collections namespace are Hash table and SortedList.
Hashtable Class
Hashtable stores items as key-value pairs. A hash table stores the key and its value as an object type.
Hashtable ht = new Hashtable(); or Hashtable ht = new Hashtable(20);
Add() method - HashTable
The Add() method adds an item with a key and value into the Hashtable. Key and value can be of any data type. Key cannot be null whereas value can be null. Add() will throw an exception if you try to add a key that already exists in the Hashtable. You can retrive the value of an existing key from the Hashtable using indexer.
Example 1: Add elements in HashTable.
Hashtable ht = newHashtable(); ht.Add(1, 10); ht.Add(2, 20); ht.Add(3, 30); ht.Add("four", "forty"); int intValue1 = (int)ht[2]; int intValue2 = (int)ht[3]; string strValue3 = (string)ht[4]; Console.WriteLine(intValue1); Console.WriteLine(intValue2); Console.WriteLine(strValue3);
30
forty
key: 1, value: 10
key: 2, value: 20
key: 3, value: 30
key: four, value: forty
Display elements from HashTable: Hashtable elements are key-value pairs stored in DictionaryEntry. So you cast each element in Hashtable to DictionaryEntry. Hashtable has a Keys and a Values property that contain all the keys and values respectively. You can use these properties to get the keys and values.
Example 2: Display elements from HashTable.
Hashtable ht = newHashtable(); ht.Add(1, 10); ht.Add(2, 20); ht.Add(3, 30); ht.Add("four", "forty"); foreach(var key in ht.keys) { Console.WriteLine(key); } foreach(var value in ht.Values) { Console.WriteLine(Value); } foreach (DictionaryEntry item in ht) Console.WriteLine("key:{0}, value:{1}",item.Key, item.Value);
2
3
four
10
20
30
forty
key: 1, value: 10
key: 2, value: 20
key: 3, value: 30
key: four, value: forty
Remove() method - HashTable
The Remove() method removes the item with the specified key from the hashtable.
Example 3: Removes element from HashTable.
Hashtable ht = newHashtable(); ht.Add(1, 10); ht.Add(2, 20); ht.Add(3, 30); ht.Add("four", "forty"); Ht.Remove("2"); foreach (DictionaryEntry item in ht) Console.WriteLine("key:{0}, value:{1}",item.Key, item.Value);
key: 3, value: 30
key: four, value: forty
Contains(), ContainsKey(), ContainsValue() - HashTable
Contains() and ContainsKey() check whether the specified key exists in the Hashtable collection. ContainsValue() checks whether the specified value exists in the Hashtable.
Example 4: Check for existing elements.
Hashtable ht = newHashtable(); ht.Add(1, 10); ht.Add(2, 20); ht.Add(3, 30); ht.Add("four", "forty"); ht.Contains(2);// returns true ht.ContainsKey(2);// returns true ht.Contains(5); //returns false ht.ContainsValue("forty"); // returns true
Clear() method - HashTable
Clear() method removes all the key-value pairs in the Hashtable.
Example 5: Removes all the key-value pairs in the Hashtable.
Hashtable ht = newHashtable(); ht.Add(1, 10); ht.Add(2, 20); ht.Add(3, 30); ht.Add("four", "forty"); ht.Clear(); // removes all elements Console.WriteLine("Total Elements: {0}", ht.Count);