This article describes about List class that is generic equivalent of ArrayList class.
In my previous article Working with Collections - ArrayList, I briefly described about ArrayList. In this article I am going to briefly describe about Generic List.
What is the problem?
In order to store ordered list of items (objects), we can simply use an ArrayList, however there are many disadvantages of using ArrayList particularly when you have a huge number of objects into it. The most important is every time you need to cast them to get your actual object and this itself is a big overhead on the application. Also there is no type safety because you can store a string, an integer as well as a custom class into a single ArrayList that will not throw any error at compile time but you will get an exception at runtime if you were intended to store only integer in the ArrayList and were casting for integer object but someone had added string type of data also.
The best solution in this scenario would be to restrict the type of object you are intended to store in your collections at the time of declaration itself so that it won't accept any other type of data.
The solution of the problem identified above can be solved by using is .NET Generics. In .NET Framework generic type exist for most of the collection classes, however we will discuss about generic type of ArrayList in this article. List
class is the generic equivalent of an ArrayList class.
Generic List Class
As an ArrayList is used to add ordered list of object so the List class is. Additionally it is used to create type-safe ordered list. Lets understand it by an example. Suppose we want to store only integer value in our collection so we will create List object by specifying the integer type for the generic parameter. For example
Adding Data into List
Note: You need to use System.Collections.Generic namespace to use .NET Generics.
List<int> list = new List<int>();
Notice the data type between less than and greather than symbol (<int>), this is how data type is declared at the time of instantiating List object. In place of int you can use any type of object, like string, datetime or your custom business objects etc.
When you compile above code it will complie properly. Now lets try to add some string into it and compile. Notice the 4th line in the below code snippet.
List<int> list = new List<int>();
If you will try to compile, you will get following error (below picture) as I have passed int as the generic parameter to the List object and trying to add a string value into it.
If this would have been an ArrayList it would have compiled properly however at run time while casting this into integer it would have thrown error (as "REA" can't be converted into integer) .
Interating through List
Iterating through List is almost similar to the ArrayList. Here as we are sure that, we have passed int as generic parameter to the list so only integer type of data would be there. So we can simply specify the data type in foreach loop and use them (no need of casting from object to integer).
// lets see how to iterate through this listint intTotal = 0;foreach (int i in list)
intTotal += i;
As against ArrayList, Generic list saves us from any potential exception that may occur at runtime. As written earlier, List is the generic equivalant of ArrayList so almost all methods that ArrayList supports (See http://www.dotnetfunda.com/articles/article140.aspx for ArrayList article) are supported by List. Apart from that there are many overloads and extra methods also that are very useful.
There are few articles I came across that are very useful on this topic and I would like to mention them for the benefits of the readers. Please feel free to respond to this article if you have some useful resources on this topic.
Thanks for reading.