How to use Hash Table,ArrayList in c# ?Explain with example.

How to use Hash Table,ArrayList in c# ?Explain with example.

ArrayList:-Arraylist is a collection of objects(may be of different types).
Arraylist is very much similar to array but it can take values of different datatypes.
If you want to find something in a arraylist you have to go through each value in arraylist, theres no faster way out.ArrayList's size can be changed dynamically.

Items are added to the ArrayList with the Add() method.
example:-HOW TO ADD DATA IN ARRAYLIST:
    using System.Collections;

class Program
{
    static void Main()
    {
        //
        // Create an ArrayList and add three elements.
        //
        ArrayList list = new ArrayList();
        list.Add("One");
        list.Add("Two");
        list.Add("Three");
    }
}
EXAMPLE-Adding one ArrayList to second one

There are different ways to add one ArrayList to another, but the best way is using AddRange. Internally, AddRange uses the Array.Copy or CopyTo methods, which have better performance than some loops.

=== Program that uses Add and AddRange ===

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        //
        // Create an ArrayList with two values.
        //
        ArrayList list = new ArrayList();
        list.Add(15);
        list.Add(17);
        //
        // Second ArrayList.
        //
        ArrayList list2 = new ArrayList();
        list2.Add(110);
        list2.Add(113);
        //
        // Add second ArrayList to first.
        //
        list.AddRange(list2);
        //
        // Display the values.
        //
        foreach (int i in list)
        {
            Console.WriteLine(i);
        }
    }
}

=== Output of the program ===

15
17
110
113
The ArrayList class provides the Count property, which is a virtual property. When you use Count, no counting is actually done; instead, a cached field value is returned

HASH TABLE:-Hashtable is also collection which takes a key corresponding to each values.
If you want to find something in a hashtable you dont have to go through each value in hashtable, instead search for key values and is faster.The Hashtable lets you quickly get an object out of the collection by using it's key.
The Hashtable object contains items in key/value pairs. The keys are used as indexes. We can search value by using their corresponding key. 
The data type of Hashtable is object and the default size of a Hashtable is 16.


Items are added to the Hashtable with the ADD()method.
Example-How to add data in hash table:--
Add method of Hashtable is used to add items to the hashtable. The method has index and value parameters. The following code adds three items to hashtable.

hshTable .Add("A1",  "Kamal");
hshTable .Add("A2",  "Aditya");
hshTable .Add("A3",  "Ashish");

Retrieving an Item Value from Hashtable

The following code returns the value of "Author1" key: 

string name = hshTable["A1"].ToString(); 

Removing Items from a Hashtable

The Remove method removes an item from a Hashtable. The following code removes item with index "A1" from the hashtable: 

hshTable.Remove("A1");


           

Looking through all Items of a Hashtable

The following code loops through all items of a hashtable and reads the values. 

// Loop through all items of a Hashtable
IDictionaryEnumerator en = hshTable.GetEnumerator();
while (en.MoveNext())
{
      string str = en.Value.ToString(); 
}

By:kamal
Date:2013-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Links

C interview questions and answers for experienced and fresher