What is difference between ArrayList & Hashtable in C#?

random

What is difference between ArrayList & Hashtable in C#?

C# - Difference between Hash Table and Arraylist?

In this article we will try to understand the difference between C# hastable and arraylist. So there are following difference between ArrayList and Hashtable :
  • A hashtable store data as name, value pair. While an ArrayList only value is store.
  • If you want to access value from the hashtable, you need to pass name. While in arraylist to access value, you need to pass index value.
about ArrayList, Hashtable, Example, difference between Array and Hashtable in c#
Hashtable and Arraylist in C#
Hence the basic difference is that HashTable represents a collection of key/value pairs that are organized based on the hash code of the key. Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be null, but a value can be. 

The ArrayList class implements a collection of objects using an array whose size is dynamically increased as required. The ArrayList class is very useful if you are working with arrays and need to add, remove, index, or search for data in a collection.
    Let try to understand the difference between ArrayList and Hashtable with example in C#. Find the code below :-

    Hastable Example C#

    using System.Collections;
    protected void Page_Load(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();

            ht.Add("01", "Rk Singh");
            ht.Add("02", "Kunal");
            ht.Add("03", "Prashant");
            ht.Add("04", "Mohan");
            ht.Add("05", "Satya");
            
            if (ht.ContainsValue("Satya"))
            {
                Response.Write("Satya is already in the list.<br/><br/>");
            }
            else
            {
                ht.Add("06", "Satya");
            }
            // Get a collection of the keys.
            ICollection key = ht.Keys;

            foreach (string k in key)
            {
                Response.Write(k + ": " + ht[k]);
            }       
        }

    OutPut
    Satya is already in the list
    04: Mohan03: Prashant02: Kunal01: Rk Singh05: Satya [post_ad]

    ArrayList Example

    using System.Collections;
    protected void Page_Load(object sender, EventArgs e)
        {
           ArrayList al = new ArrayList();

                Console.WriteLine("Adding some numbers:");
                al.Add(11);
                al.Add(42);
                al.Add(33);
                al.Add(21);
                al.Add(35);
                al.Add(46);
                al.Add(77);

                Response.Write("Capacity: {0}" + al.Capacity + "<br/>");
                Response.Write("Count: {0}" + al.Count + "<br/>");
                         
                Response.Write("Content: ");
                foreach (int i in al)
                {
                    Response.Write(i + " ");
                }
               
                Response.Write("<br/>Sorted Content: ");
                al.Sort();
                foreach (int i in al)
                {
                    Response.Write(i + " ");
                }           
            }

    Output
    Capacity: {0}8
    Count: {0}7
    Content: 11 42 33 21 35 46 77
    Sorted Content: 11 21 33 35 42 46 77 



    I hope you will enjoy the C# interview tips while programming with C sharp. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. 

    Also If you like this article, don't forget to share this article with your friends and colleagues.
    What is difference between ArrayList & Hashtable in C#? Reviewed by Ravi Kumar on 11:49 PM Rating: 5

    2 comments:

    All Rights Reserved by Etechpulse © 2012 - 2017

    Contact Form

    Name

    Email *

    Message *

    Powered by Blogger.