Translate

Monday, August 22, 2011

C# : Difference between Array and Arraylist



System.Array
System.Collections.ArrayList
1
Arrays are strongly typed.

 That is all the elements have to be of the same type. For example an array of integers has to have all integers.  It cannot have a mix of integers and strings.
An arraylist is NOT strongly typed.

You can have a  combination of built in types  (int,string etc) in your arraylist.
2
Cannot be dynamically resized.

(Note :Array.Resize() doesnot actually resize the existing array. It infact creates a new array with the required length and copies values from the old array to new array. Using the Resize is more memory intensive than AddRange() in arraylist)
Can be dynamically resized using the method Arraylist.AddRange()
3.
Eg
int[] myIntArray = new int[2]
myIntArray[0]=10;
myIntArray[1]=20;


Eg
ArrayList leaderNames = new ArrayList();

 leaderNames.Add("Obama");
 leaderNames.Add(5);



4 comments:

Comments will appear once they have been approved by the moderator