In this article I will explain concept of hashtable and arrayList , its uses and the differences between hashtable and arrayList

ArrayList is a non-generic type of collection. It can contain element of any data type. It is in ordered collection and can be accessed using index.


Now take an example of arraylist for understanding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
 
namespace ConsoleApplication10
{
      
 
    class TestMyMath
    {
        public static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add(1); //integer type
            list.Add('A'); //character
            list.Add("Abc");//string type
 
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);//getting element thorough index
            }
 
 
        }
    } 
  
}
 

Hashtable

Hashtable collection stores key value pair. It represents a collection of key & value that are organized based on the hash code of key and value pair. Key and value can be of any datatype key cannot be null where as value can be null. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
 
namespace ConsoleApplication10
{
   
  
 
    class TestMyMath
    {
        public static void Main(string[] args)
        {
            Hashtable hash = new Hashtable();
            hash.Add("100", "Employee1");
            hash.Add("101", "Employee2");
            hash.Add("102", "Employee3");
            hash.Add("A", "Employee4");
            if (hash.ContainsKey("100"))
            {
                Console.WriteLine("This key item is already present");
            }
            else
                hash.Add("100","Employee5");
         
 
 
        }
    } 
  
}



ArrayList and Hashtable


Example for checking element

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
 
namespace ConsoleApplication10
{
   
  
 
    class TestMyMath
    {
        public static void Main(string[] args)
        {
            Hashtable hash = new Hashtable();
            hash.Add("100", "Employee1");
            hash.Add("101", "Employee2");
            hash.Add("102", "Employee3");
            hash.Add("A", "Employee4");
            if (hash.ContainsKey("103"))//checking element using key
            {
                Console.WriteLine("This key item is already present");
            }
            if (hash.ContainsValue("Employee5"))//checking element using value
            {
                Console.WriteLine("This value item is already present");
            }
            else
            {
                hash.Add("103","Employee5");
                Console.WriteLine("employee added");
            }
 
         
 
 
        }
    } 
  
}


ArrayList and Hashtable


Accessing all element 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
 
namespace ConsoleApplication10
{
   
  
 
    class TestMyMath
    {
        public static void Main(string[] args)
        {
            Hashtable hash = new Hashtable();
            hash.Add("100", "Employee1");
            hash.Add("101", "Employee2");
            hash.Add("102", "Employee3");
            hash.Add("A", "Employee4");
            if (hash.ContainsKey("103"))//checking element using key
            {
                Console.WriteLine("This key item is already present");
            }
            if (hash.ContainsValue("Employee5"))//checking element using value
            {
                Console.WriteLine("This value item is already present");
            }
            else
            {
                hash.Add("103","Employee5");
                Console.WriteLine("employee added");
            }
 
            ICollection key = hash.Keys;
 
            foreach (string k in key)//showing all item after adding
            {
                Console.WriteLine(k + ": " + hash[k]);
            }
 
            Console.ReadKey();
 
 
        }
    } 
  
}



ArrayList and Hashtable



Difference between Hashtable and Arraylist

  • Hashtable store data as name and value pair.
  • Array list only store value.
  • Array list have numeric index.
  • Hashtable can have string index.
  • In hashtable we can store different type of data int , string etc.

You can vist these useful related post.

What are differences between Array list and Hash table

ArrayList Class in C# 

  Modified On Mar-17-2018 05:15:54 AM

Leave Comment