difference between arraylist and list collection

Problems with ArrayList

As we have seen in an earlier topic, ArrayLists are used to store any data which is inherited from System.Object. Which means, we can store an integer, string and custom object in a single ArrayList. To get the actual object, we need to cast them. This takes a performance hit when an ArrayList holds large number of objects.

Another disadvantage of ArrayLists is that there is no type safety. If we intended to create an ArrayList with only integers, and an attempt is made to add string data to the ArrayList, no compile time error is generated. ArrayList can hold different types of data, which will return an error at runtime. This is because we write code to work with one type of data. The solution to this problem is to restrict the type of data we can store in a collection and ensure that the collection contains only the required type of data.


The .Net framework provides the Generic type for collection classes and these classes ensure that a specific type of data can be stored in the collection. Now, we will discuss the generic type of ArrayList, i.e. List <T> class. Generics are the most powerful feature of C# 2.0. Generics allow us to define type-safe data structures. Generic collection types also generally perform better than the corresponding nongeneric collection types.

A generic collection is strongly typed (type safe), meaning that we can store only one type of data into the collection. This ensures there are no type mismatches at runtime. An additional benefit of generic collection is that performance is better because we do not need to converted them to actual object.

Suppose we want to store only integer values in a collection, we will specify the integer type for the generic parameter. The below code shows how to create a List. Note the data type between less than and greater than symbol <int>. This is how data type is declared at the time of instantiating the List object. In place of int we can use any type of object, like string or a custom business object.

List<int> myInts = new List<int>();