Basic Difference between ArrayList and List

Lets find out some important differences for two collection types – ArrayList & List<T> , both are designed / exist in framework for grouping the objects together and perform various operations on them as per our requirments.

ArrayList –

1) Namespace System.Collections contain ArrayList ( This namespace is added by default when we we creat any aspx page in C#)

2) Create ArrayList :

ArrayList stringArrayList = new ArrayList();

Here we dont need to specify object type arraylist going to contain,store different type of objects/items/

3) In ArrayList each item is stored as an Object so while reteriving we can get object only.

4) It is like Array of Objects.


List<T> –

1) Namespace System.Collections.Generic List<T> ( we need to add namespace if we want to use Generic Types)

2) Create List<T>:

List<string> stringList = new List<string>();

i.e.

List<type> nameOfList = new List<type>(); & type means object type which List<T> going to hold.

3) In List<T> , it can hold/contain only type of object which is mentioned while initialising the List<T>

Just like in above example stringList will only contain object of type string, the type supplied as generic parameter.

4) It is newly added in .Net 2.0 & onwards, fast processing no need of casting explicitly.

Lets see one example.

string first = “First String”;

string second = “Second String”;

int firstInt = 1;

int secondInt = 2;

ArrayList stringArrayList = new ArrayList(); // No need to specify the object type,can store anything.

stringArrayList.Add(first); // string type

stringArrayList.Add(second); // string type

stringArrayList.Add(firstInt); // Can contain any type so no problem in storing int


List<string> stringList = new List<string>(); // Need to specify the object type, will contain only string types.

stringList.Add(first);

stringList.Add(second);

1) Lets consider Case :

stringList.Add(firstInt); // adding int in string type List.

we will get the exceptions below as List need to contain only string types. others are not allowed.

1) ‘The best overloaded method match for ‘System.Collections.Generic.List<string>.Add(string)’ has some invalid arguments’

2) ‘Argument ‘1’: cannot convert from ‘int’ to ‘string’

2) Lets consider another case :

string abcd = stringArrayList[1];

Suppose if we try to get an ArrayList item without using the casting then we can get following exception, as arraylist contain Objects only we need to cast as pre our requirments.

We will get Exception –

‘Cannot implicitly convert type ‘object’ to ‘string’. An explicit conversion exists (are you missing a cast?)’

We can avoid it as

string abcd = stringArrayList[1].ToString(); // Needs casting ,memory overhead.

Advertisements

21 thoughts on “Basic Difference between ArrayList and List”

  1. @@Muhammad El-Nabawy: Yes there is a performance issue,List
    is more faster than the ArrayList due to its Genric nature
    in ArraryList you have to cast Object Type to a specific type(Unboxing)
    && There is nothing like Boxing & Unboxing with List

  2. thanks its nice explanations, But i have one Qustion as you say Listhas no need of boxing & unboxing like Arraylist means that it is not the supper type Object type, So please tell me what is the actual type of List .

  3. Its good explanations, But I have a Question. the Object Type is not the type of List and why Boxing-Unboxing is not needed in List.

  4. I Think post, “Basic Difference between ArrayList and List | CollectedDotNet” was in fact spot on!
    I actuallycan’t see eye to eye together with you more! Finally looks like I reallylocated a blog website really worth reading through. Many thanks, Maxwell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s