Working with System.Collections.ArrayList in C#

System.collections is an important and useful namespace which provides several classes and interfaces for the management of collections of different types. Of which IList, ArrayList, Hastable, IEnumerator and IComparer are a few to name. We can find these interfce and clases at the root of many classes found in later libraries released with later versions of .Net Framework. We will look into these classes and interfaces and where possible, try to see what methods are available and what can we do with them.

To start with, we will take ArrayList and see what it has to offer.

ArrayList is generally not recommended as it is not strongly typed and there are better options available, which we will discuss in later posts. ArrayList might only be be helpful if in case one is not sure of the type up-front for the collection.

ArrayList class provides three constructors, as follows

ArrayList arrayList;

// an empty constructor
arrayList = new ArrayList();

// constructor that takes another collection
arrayList = new ArrayList(new ArrayList());

// constructor which takes an int as initial capacity
arrayList = new ArrayList(0);

To Add Items To ArrayList:

// Add items in a ArrayList

In code above you might have noticed that, different types of objects are assigned to same collection, making it prone to errors and adding over head for type checks and cast To and From Object while carrying out operation like

if(arrayList[0].GetType()== typeof(string))
   string valueAt0 = (string)arrayList[0];

Following are the remaining methods with usage samples

            // search in an ArrayList

            // get capacity of an ArrayList
            int listCapacity = arrayList.Capacity;

            // use Cast<T> to convert to another Collection
            List<string> list = arrayList.Cast<string>().ToList();

            // clear an Arraylist 

            // Check if Arraylist contains an object

            // copy the contents into an Array
            string[] tempArr = new string[3];
            arrayList.CopyTo(tempArr, 0);

            // get total number of items in ArrayList
            int numberOfItems = arrayList.Count;

            // get the index of an object in ArrayList

            // Get and use IEnumerator from Arraylist
            IEnumerator e = arrayList.GetEnumerator();
            while (e.MoveNext())
                Object obj = e.Current;

            // get a new ArrayList from range of objects
            arrayList = arrayList.GetRange(0, 3);

            // get type 

            // inset an object into ArrayList at a location:
            arrayList.Insert(0, "zero");

            // inset a range of objects into ArrayList at:
            arrayList.InsertRange(0, arrayList);

            // check , if Arraylist is of fixed size
            bool IsFixedSize = arrayList.IsFixedSize;

            // check , if Arraylist is read only
            bool IsReadOnly = arrayList.IsReadOnly;

            // remove an object 

            // remove an object from index

            // sort ArrayList

            // convert to an Array of Type T

            // get a string representation

One thought on “Working with System.Collections.ArrayList in C#

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s