Working with Collections - Generic List

SheoNarayan
Posted by in C# category on for Beginner level | Views : 20733 red flag
Rating: 1 out of 5  
 1 vote(s)

This article describes about List class that is generic equivalent of ArrayList class.
Introduction

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.


Generics
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>();

list.Add(1);

list.Add(2);

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>();

list.Add(1);

list.Add(2);

list.Add("REA"); 

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 list

int 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.

Useful Resources

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.

http://www.ondotnet.com/pub/a/dotnet/2005/06/20/generics.html?page=2
http://www.developer.com/net/net/article.php/11087_3499301_2
http://msdn.microsoft.com/en-us/library/6sh2ey19(VS.80).aspx 

Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)