Wednesday, August 6, 2008

Arraylist

Arraylist implements from Ilist Interface using array and can be increased dynamically.
Arraylist uses the namespace System.Collections.
The Capacity property of Arraylist holds number of elements in the Arraylist. With the help of Add method you can able to add an item in the Array list.

C# Code
----------------------------------------------------
Arraylist arrList = new Arraylist();
arrList.Add("Kumar");
arrList.Add("Venky");
-----------------------------------------------------

As the elements are added to a Arraylist, the capacity is increased automatically through rellocation.It can be decreased with the help of TrimToSize()

Arraylist allows duplicate elements and accepts null value.
You must sort the Arraylist explicitly before performing operation such as Binary search for which sorting is needed.

IsFixedSize property gets a value indicating whether ArrayList object has a fixed size.

Don't mix the content of the arraylist. Below example helps you to understand

------------------
C# Code
ArrayList contentMixing = new ArrayList();

//Adding some numbers to the list
contentMixing.Add(10);
contentMixing.Add(20);

//Adding some text to the list
contentMixing.Add("Kumar");
contentMixing.Add("Venky");

//While looping gives error
foreach(string mixedText as contentMixing)
{
//Throws exception
}
------------------


Kumar