Difference between Arraylist and Generic List

Difference between Arraylist and Generic List


private void button1_Click
(object sender, EventArgs e)
{
//Arraylist - Arraylist accept values as object
//So i can give any type of data in to that.
//Here in this eg: an arraylist object accepting
//values like String,int,decimal, char and a custom object

Employee emp = new Employee("2", "Litson");
ArrayList arr = new ArrayList();
arr.Add("Sabu");
arr.Add(234);
arr.Add(45.236);
arr.Add(emp);
arr.Add('s');
//This process is known as boxing

//To get inserted vales from arraylist we have to specify the index.
//So it will return that values as object.
//we have to cast that value from object to its original type.

String name = (String)arr[0];
int num = (int)arr[1];
decimal dec = (decimal)arr[2];
Employee em = (Employee)arr[3];
char c = (char)arr[4];
//This process that is converting from object to its original type is known as unboxing
//------------------------------------------------------------------------------------
//Generic List
//List<>

//Main advantage of Generic List is we can specify the type of data we are going to insert in to
//List. So that we can avoid boxing and unboxing
//Eg:
List<String> strLst = new List<string>();
strLst.Add("Sabu");//Here List accepting values as String only
strLst.Add("Litson");
strLst.Add("Sabu");
strLst.Add("Sabu");

List<int> intLst = new List<int>();
intLst.Add(12);//Here List accepting values as int only
intLst.Add(14);
intLst.Add(89);
intLst.Add(34);

List<decimal> decLst = new List<decimal>();
decLst.Add(2.5M);//Here List accepting values as deciaml only
decLst.Add(14.4587m);
decLst.Add(89.258m);
decLst.Add(34.159m);

List<Employee> empLst = new List<Employee>();
empLst.Add(new Employee("1", "Sabu"));//Here List accepting Employee Objects only
empLst.Add(new Employee("2", "Mahesh"));
empLst.Add(new Employee("3", "Sajith"));
empLst.Add(new Employee("4", "Binu"));


//To get values from Generic List
String nme = strLst[0]; //No need of casting
int nm = intLst[0];
Decimal decVal = decLst[0];
Employee empVal = empLst[0];
}


Comments

Author: Rajaram Murali23 Sep 2008 Member Level: Bronze   Points : 0

Very Good

Author: satya25 Nov 2009 Member Level: Gold   Points : 1

Hi Sabu,

Nice resource to understand the difference between collections and generics.

Keep posting good resources like this.


Regards,
Satya.

Author: Pandu (srini)29 Nov 2009 Member Level: Gold   Points : 0

HI,
Good posting adn well explanation

Author: SuganyaCMR16 Dec 2009 Member Level: Gold   Points : 0

Hi,

Really useful article for me..
Thanks a lot sabu..

Author: vasanth16 Aug 2010 Member Level: Silver   Points : 0

HI,
well explanation

Author: Binu27 Aug 2010 Member Level: Bronze   Points : 0

Good

Author: Pal (Parthiv) Patel31 Aug 2010 Member Level: Gold   Points : 1

----------1111111111-------------------
hey
this one is awesome code snippet
keep it up
put good codes and earn money and points
------------!!!!!!!!!!-------------------

Guest Author: yugender28 Feb 2012

very good artical

Guest Author: Anil29 Mar 2012

very good .

Guest Author: Neeraj03 Jun 2012

Thanx really nice article.......

Guest Author: santhi02 Nov 2012

nice code,
happy coding !!

Guest Author: Sankar NavaneethaKri12 Feb 2013

very nice with example



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: