Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / Visual Basic
Tip/Trick

What is the Difference between ArrayList and List Class?

Rate me:
Please Sign up or sign in to vote.
4.92/5 (28 votes)
21 Dec 2014CPOL2 min read 182.3K   17   17
ArrayList is a non-generic collection class and resides in System.Collection namespace whereas List is a generic class and resides in System.Collections.Generic namespace

Background

This is one of my favorite interview questions. This question helps me to find out if candidate has good knowledge of generics or not. On the internet, there are a number of writeups on "difference between Array and ArrayList" but I didn't find any on "difference between ArrayList and List", so I am posting one...

Must Know

First, one should know what is upcasting? Upcasting is converting derived type into base type. In .NET, all data-types are derived from Object. So we can typecast any type to Object type. For example, if Customer is class, then we can create object of Customer like this:

C#
Object cust = new Customer()

Here new Customer() will create object on heap and its address we are putting in reference variable of type Object.

Image 1

ArrayList

C#
ArrayList marks = new ArrayList();
marks.Add(50);
marks.Add(70.5);
marks.Add("Sixty");

In the above code snippet, we are creating object of ArrayList and adding different type of data in it. But actually ArrayList is a collection of Object type, and when we add any item to ArrayList, it first converts it to object type (upcasting) and then adds it to collection object.

Interesting Fact: As ArrayList can only create collection for Object type, it is said to be non-generic class. It might be confusing as it seems that we can add any datatype value like int, float, string to ArrayList collection so in that sense it should be called as generic class. But in fact, it internally converts all these datatypes in object type and then adds to collection.

We can visualise it as:

Image 2

List

C#
List<int> marks = new List<int>();

marks.Add(50);
marks.Add(70);
marks.Add(60);

In the above snippet, we can observe that while creating object of list class, we have mentioned datatype of collection we want to create. We need to pass datatype while creating object as List class doesn’t hard code it internally. So the above declaration will create marks as collection of integers; and not collection of objects as in case of ArrayList.

We can visualize it as:

Image 3

Interesting fact: In the above collection “marks” you can only add integers and no other type. In that sense, it should be referred to as non-generic! But wait, using the same List class, you can also create collection of string type:

C#
List<string> names = new List<string>();

Or even you can create collection of custom types. For example, collection of Student type can be created as:

C#
List<Student> students = new List<Student>(); 

And as using same List class, now you are able to create collection of any data-type as integers, strings or students; this class is known as Generic class.

One of the benefits of using generic collection is no need of boxing and unboxing while tackling with collections of value types.

We can visualise List of string type (or any ref type) as:

Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
13+ Years of Experience. Seen death and rise of various technologies. Started with VB - 6.0 back in 2004, then updated to .NET somewhere in 2008. used Win Forms, ASP.NET Web Forms. Switched to ASP.NET MVC in 2013 for very short time. Right now using ASP Core since 2015 when it was in RC 1.

Comments and Discussions

 
Questionwhat is better between list of objects and arrayList Pin
Member 1405651515-Nov-18 23:19
Member 1405651515-Nov-18 23:19 
GeneralMy vote of 5 Pin
Member 133387112-Jun-18 0:32
Member 133387112-Jun-18 0:32 
QuestionUse of ArrayList Pin
Member 228277930-Jun-16 0:27
Member 228277930-Jun-16 0:27 
AnswerRe: Use of ArrayList Pin
vaynenick6-Oct-16 20:52
vaynenick6-Oct-16 20:52 
GeneralModifying object in list. Pin
Member 228277930-Jun-16 0:25
Member 228277930-Jun-16 0:25 
Praiseacknowledge Pin
Member 12452061-Vishal20-Apr-16 15:01
Member 12452061-Vishal20-Apr-16 15:01 
QuestionWhat If, if I add generic to ArrayList Pin
Member 121044841-Nov-15 3:19
Member 121044841-Nov-15 3:19 
AnswerRe: What If, if I add generic to ArrayList Pin
Member 121044841-Nov-15 3:22
Member 121044841-Nov-15 3:22 
GeneralIt still seems backwards - that Lists are Generic Pin
smhiker11-Sep-15 9:41
smhiker11-Sep-15 9:41 
GeneralRe: It still seems backwards - that Lists are Generic Pin
Member 1306353616-Mar-17 5:08
Member 1306353616-Mar-17 5:08 
QuestionGood One Pin
brainysaki21-Jun-15 12:31
brainysaki21-Jun-15 12:31 
QuestionAvoid ArrayList Pin
Andreas Haeni26-Dec-14 23:34
Andreas Haeni26-Dec-14 23:34 
What's the benfit of using an ArrayList in the context of solid, object-oriented software development? I have never found one. IMHO, ArrayLists are useless and should be forbidden.

Use a generic List<> or create custom collections, if you want to handle a list of identical types as an entity.
Create an object with properties if you want to handle different types as an entity.
GeneralMy vote of 3 Pin
nam123456722-Dec-14 13:30
professionalnam123456722-Dec-14 13:30 
GeneralMy vote of 3 Pin
NGzero22-Dec-14 13:11
professionalNGzero22-Dec-14 13:11 
QuestionEquivalent in VB Pin
PlazmaSoft21-Dec-14 21:16
PlazmaSoft21-Dec-14 21:16 
AnswerRe: Equivalent in VB Pin
Bhushan Mulmule21-Dec-14 22:11
Bhushan Mulmule21-Dec-14 22:11 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun21-Dec-14 19:01
Humayun Kabir Mamun21-Dec-14 19:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.