In this article I will explain difference between ArrayList and Generic List in C# .Net and VB.Net.
ArrayList
1. ArrayList belongs to the System.Collections namespace, i.e. you need to import the following namespace.
C#
using System.Collections;
VB.Net
Imports System.Collections
 
2. ArrayList does not have type restriction for storing data i.e. it is not Type Safe. You can store anything in ArrayList. In fact same ArrayList can store multiple types of objects.
C#
ArrayList arrList = new ArrayList();
arrList.Add(921);
arrList.Add("Mudassar Khan");
arrList.Add(DateTime.Now);
VB.Net
Dim arrList As New ArrayList()
arrList.Add(921)
arrList.Add("Mudassar Khan")
arrList.Add(DateTime.Now)
 
3. ArrayList stores all data as object thus to get it back you must remember what you stored where and correspondingly Type Cast it as per its original Type when stored.
C#
int number = Convert.ToInt32(arrList[0]);
string name = arrList[1].ToString();
DateTime dt = Convert.ToDateTime(arrList[2]);
 
VB.Net
Dim number As Integer = Convert.ToInt32(arrList(0))
Dim name As String = arrList(1).ToString()
Dim dt As DateTime = Convert.ToDateTime(arrList(2))
 
4. ArrayList is mainly for .Net 2.0 Framework projects as during that period Generic List was not invented.
5. While running a Loop on ArrayList you need to use Object data type. Thus this is another disadvantage as you again do not know what type of data particular item contains.
C#
foreach (object o in arrList)
{
 
}
VB.Net
For Each o As Object In arrList
 
Next
 
 
Generic Lists (List<T>)
1. Generic List (List<T>) belongs to the System.Collections.Generic namespace, i.e. you need to import the following namespace.
C#
using System.Collections.Generic;
VB.Net
Imports System.Collections.Generic
 
2. In Generic List (List<T>), T means data type, i.e. string, int, DateTime, etc. Thus it will store only specific types of objects based on what data type has been specified while declarations i.e. it is Type Safe. Thus if you have a Generic List of string you can only store string values, anything else will give compilation error.
Below I had no option other than having three different Generic Lists for three different data types.
C#
List<string> lstString = new List<string>();
lstString.Add("Mudassar Khan");
lstString.Add("Robert Hammond");
lstString.Add("Ramesh Singh");
 
List<int> lstInt = new List<int>();
lstInt.Add(991);
lstInt.Add(10);
lstInt.Add(4450);
 
List<DateTime> lstDateTime = new List<DateTime>();
lstDateTime.Add(DateTime.Now);
lstDateTime.Add(DateTime.Now.AddDays(20));
lstDateTime.Add(DateTime.Now.AddHours(-10));
VB.Net
Dim lstString As New List(Of String)()
lstString.Add("Mudassar Khan")
lstString.Add("Robert Hammond")
lstString.Add("Ramesh Singh")
 
Dim lstInt As New List(Of Integer)()
lstInt.Add(991)
lstInt.Add(10)
lstInt.Add(4450)
 
Dim lstDateTime As New List(Of DateTime)()
lstDateTime.Add(DateTime.Now)
lstDateTime.Add(DateTime.Now.AddDays(20))
lstDateTime.Add(DateTime.Now.AddHours(-10))
 
3. Generic List stores all data of the data type it is declared thus to getting the data back is hassle free and no type conversions required.
C#
int number = lstInt[0];
string name = lstString[0];
DateTime dt = lstDateTime[0];
VB.Net
Dim number As Integer = lstInt(0)
Dim name As String = lstString(0)
Dim dt As DateTime = lstDateTime(0)
 
4. Generic List must be used instead of ArrayList unless specific requirement for projects higher than .Net 2.0 Framework.
5. While running a Loop on Generic List again it is problem free as we exactly know what the List contains.
C#
foreach (int number in lstInt)
{
}
foreach (string name in lstString)
{
}
foreach (DateTime dt in lstDateTime)
{
}
VB.Net
For Each number As Integer In lstInt
Next
For Each name As String In lstString
Next
For Each dt As DateTime In lstDateTime
Next
 
Thus ending this article, I would recommend to use Generic List as my personal opinion if you’re your projects use Framework higher than .Net 2.0.