C# Difference between Arraylist and Hashtable with Examples

  By : Suresh Dasari
  Posted On : 02-May-2023

Here we will learn what is arraylist in c#, uses of arraylist in c#, what is hashtable in c#, uses of hashtable in c#, and the differences between arraylist and hashtable with examples.

What is ArrayList in C#?

In c#, arraylist is useful to store the collection of elements of different data types. The size of arraylist will vary dynamically based on the elements added or deleted from the arraylist.

 

Following is the syntax of defining the arraylist in c#.

 

ArrayList lst = new ArrayList();

In c#, you can access arraylist elements either by using loops (for and foreach) or index positions. While accessing arraylist elements you need to cast ArrayList items into appropriate data types before using them in an application.

 

Following is the example of defining, adding elements, and accessing the elements from ArrayList in c#.

 

// creating arraylist
ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5f);
arrlist.Add("Tutlane");
// accessing the first element
string msg = (string)arrlist[0];
//Accessing the third element
float num = (float)arrlist[2];

To learn more about arraylist, visit ArrayList in C# with Examples.

What is Hashtable in C#?

In c#, the Hashtable is useful to store the collection of key/value pair elements of different data types. To use hashtable items in your application, you need to cast hashtable items into appropriate data types same as arraylist.

 

Following is the syntax of defining the hashtable in c#.

 

Hashtable tbl = new Hashtable()

To access hashtable items, you need to use the DictionaryEntry object to get the key/value pair elements.

 

Following is the example of adding and accessing the elements from Hashtable in c#.

 

Hashtable htbl = new Hashtable();
htbl.Add("msg", "Welcome");
htbl.Add("site", "Tutlane");
htbl.Add(1, 20.5);
// Accessing Hashtable elements
foreach (DictionaryEntry item in htbl)
{
Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
}

To learn more about Hashtable, visit Hashtable in C# with Examples.

Difference between ArrayList and Hashtable in C#

As discussed, both arraylist and hashtable are useful to store the collection of elements of different data types. The following table lists the differences between arraylist and hashtable in c#.

 

ArrayListHashTable
The arraylist is useful to store the collection of elements of different types. The hashtable is useful to store the collection of key/value pairs of different types.
The arraylist elements can be accessed either by using loops or index values. The hashtable elements can be accessed by using a key or DictionaryEntry object.
To use arraylist elements, you need to cast it to appropriate data types. Same as arraylist, you need to cast hashtable items to the appropriate type to use it in the application.
The performance of the arraylist is less because it will iterate through the collection of items. The hashtable uses a hash function to find the elements with a key so it will return the data faster.
In arraylist you are allowed to add duplicate values. In hashtable, the value can be duplicated but the key shouldn't be duplicated.

 To learn more about arraylist, hashtable, and other c# topics, visit our C# Tutorial.