Archive

Posts Tagged ‘C# collections’

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

ArrayList in C#

July 22, 2015 Leave a comment

Benefits of Array List

  1. Insert Elements: An Array List starts with a collection containing no elements. You can add them in any position as you choose them.
  2. Automatic Resizing: you do not need to specify the array size; as you add elements, the array automatically ensures there is enough memory for the array.
  3. Flexibility When Removing Elements: you can remove any element from an Array list very easily.

Limitations of Array List

The flexibility of Array List comes at a cost of performance. Since memory allocation is a very expensive business, the fixed number of elements of the simple array
makes it much faster to work with.

Example:

ArrayList obj = new ArrayList();
obj.Add(“item1”);
obj.Add(“2”);
obj.Add(“Delhi”);

Obj.Insert(2,”item2″);

Obj.Remove(“item1”);
Obj.RemoveAt(3);

%d bloggers like this: