Archive

Posts Tagged ‘Benifites of Hashtable’

Hashtable in C#

July 22, 2015 Leave a comment

Hashtable objects are quite similar to an ArrayList object except that it is not required to use a numerical index. Instead, we can use a texture key that can be
numeric.

Benefits of Hashtable

  1. Insert Elements: you can add as many pairs of key/value elements as required. You do not have to specify the size ahead of time.
  2. Non-numeric index: you can use text, number and dates as your key (index).
  3. Faster Lookup: the hashtable collection caters very fast lookup of elements.
  4. Flexibility when Removing elements: you can remove any element from an Arraylist very easily

Limitations of Hashtable

  • Key must be unique: The Hashtable stipulates the key uniqueness requirement that is very cumbersome to manipulate.
  • No useful sorting: Sorting is not done by using keys or values. The items in the Hashtable are sorted internally to make it easy to find objects very quickly.
  • Performance: Although the lookup is very quick in a Hashtable, the CLR must do quite a lot of work to maintain them that is very resource-intensive.

Hashtable obj = new Hashtable();
Obj[“in”] = “india”;
Obj[“en”] = “England”;
Obj[“us”] = “USA”;

Obj[Convert.ToDateTime(“21/12/2012”)] = “The Judgment day”;

string Country = Convert.ToString(obj[“in”]);

Advertisements
%d bloggers like this: