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
arrayList.Add("one");
arrayList.Add(1);
arrayList.Add(true);

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
            arrayList.BinarySearch("one");

            // 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 
            arrayList.Clear();

            // Check if Arraylist contains an object
            arrayList.Contains("one");

            // 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
            arrayList.IndexOf("one");

            // 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 
            arrayList.GetType();

            // 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 
            arrayList.Remove("one");

            // remove an object from index
            arrayList.RemoveAt(0);

            // sort ArrayList
            arrayList.Sort();

            // convert to an Array of Type T
            arrayList.ToArray(typeof(string));

            // get a string representation
            arrayList.ToString();
Advertisements

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:

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