List and ArrayList Most Asked Question in Interview

Recently, while I was working on my project using “List” and “ArrayList”, I realized that an interviewer might ask about the differences between them. What are the key distinctions between “List” and “ArrayList”?

First, to understand the differences between “List” and “ArrayList”, it's essential to know why we need them.

<List> is a generic collection that represents a strongly typed list object that is accessed by their index.

Note. The index starts with 0.

Now, I have a question why is there an array that can collect a number of records accessed by index?

So, the main difference is that where the array is fixed in size, you have to fix its size before storing data in an array. But in List, you can store a number of records as you want; simply declare it without specifying any size.

So, the Conclusion is that the array must specify size; the List is not, so you can say the array is not dynamic and the list is dynamic in storage.

Let’s understand the below example.

syntax

int[] arr = new int[5]; // Array declaration and size assignment

// Assign values
arr[0] = 100;
arr[1] = 101;
arr[2] = 102;
arr[3] = 103;
arr[4] = 104;

// Display values
Console.WriteLine("Length of the array is {0}", arr.Length); 
// This line displays the output of the array length.

Console.ReadKey(); 
// Program will terminate after any keyboard input, holding the output for display.

Output

The length of the array is 5

To print any index value, you can simply write this line.

// Display values
Console.WriteLine("Array of [0]th index value is {0}", arr[0]);

Output

array of [0]th Index value is 1

If you want to print all records at a time, you have to print in the loop, as you know from the example below.

// Display values
for (int i = 0; i < arr.Length; i++) 
{
    Console.WriteLine("array of [{0}]th Index value is {1}", i, arr[i]);
}

Output

array of [0]th Index value is 1
array of [1]th Index value is 2
array of [2]th Index value is 3
array of [3]th Index value is 4
array of [4]th Index value is 5

Let’s go in List.

List<int> list = new List<int>(); // List Declaration

// Assign values
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);

// Display values
Console.WriteLine("List of [0]th Index value is {0}", list[0]);

Output

The list of [0]th Index value is 1

To print all values in the list the same as the array.

for (int i = 0; i < list.Count; i++)
{
    Console.WriteLine("List of [{0}]th Index value is {1}", i, list[i]);
}

Output

List of [0]th Index value is 1
List of [1]th Index value is 2
List of [2]th Index value is 3
List of [3]th Index value is 4
List of [4]th Index value is 5

So, you can clearly see in both examples where the array is mandatory to define their size, and the list is not.

So, the list is a dynamic and strong type, but there is a limitation of the list is where in the. List, you can store similar type data only, but in the real world, what happens when you have to store a person's details like ID, name, gender, salary, pin code, etc. So, .NET introduces a new list, which is “ArrayList” which can store real word data.

Let’s check ArrayList by example.

// Declare ArrayList
ArrayList arrayList = new ArrayList();

// Assign Values
arrayList.Add(100);
arrayList.Add("Rahul Chauhan");
arrayList.Add("Male");
arrayList.Add(2000.50);
arrayList.Add(275205);

// Display the first element
Console.WriteLine("ArrayList of [0]th Index value is {0}", arrayList[0]);

Output

ArrayList of [0]th Index value is 100

// Print all records similar to array and list
for (int i = 0; i < arrayList.Count; i++)
{
    Console.WriteLine("ArrayList of [{0}]th Index value is {1}", i, arrayList[i]);
}

Output

ArrayList of [0]th Index value is 100
ArrayList of [1]th Index value is Rahul Chauhan
ArrayList of [2]th Index value is Male
ArrayList of [3]th Index value is 2000.5
ArrayList of [4]th Index value is 275205

So, the ArrayList is a no-generic collection provided by the .NET Framework, which is dynamic in size array object. You can easily add, remove, and access elements by using their index.

ArrayList can store elements of different types. But in List, you have to store elements only similar type of data only.

Conclusion

  • ArrayList and List are both dynamic in size.
  • ArrayList and List are both collection types where you can add, remove, and access data easily.
  • ArrayList and List both are dynamic in size but not collection of data, whereas in List, you can store only similar types of data, but in ArrayList, you can store similar and different types of data also.

I hope you will understand and enjoy this blog, you have any doubts, queries, or suggestions related to this blog, feel free to ask!

Next Recommended Reading Arrays And ArrayList Quick Look