C# collections-Array List, An Intro to dynamic arrays

Array List is a C# collection that is classified as a non-generic collection. Unlike static arrays, this type of collection can dynamically increase or shrink based on the need.

We need to add a new namespace to our program when we try out the Array List which is called “System.Collections”. I will discuss on how one can create an Array List, access, insert into, remove and many more operations.

Create an array list

Array List constructor can be invoked in various ways. The most simple and well known is the one shown below.

static void ArrayListTutorial()
{
      ArrayList arrList = new ArrayList();
      var arrLst = new ArrayList();
      ArrayList messageArrList = new ArrayList() 
                                        {
                                            "Good", 
                                            "Morning", 
                                            "Have a good day"
                                        };
}

When we create ArrayList using new ArrayList() constructor. The return type is the “ArrayList” type. We can also hold the variable with the type ‘var’ which automatically deduces its type. The array list can be created in other ways by using an ICollection object or using the capacity of the array at instantiation.

Add elements into an Array list

We can add to an array list in various ways. As you see in the previous example we added when we created the ArrayList. The other ways are as shown in the example below. This one is a modification of the example above.

static void ArrayListTutorial()
{

    ArrayList arrList = new ArrayList();
    var varArrLst = new ArrayList();
    ArrayList messageArrList = new ArrayList()
                                        {
                                            "Good", "Morning", "Have a good day"
                                        };

    arrList.Add(1);
    arrList.Add(90);
    arrList.Add(1000);

    messageArrList.Add("happy Coding");

    varArrLst.AddRange(arrList);

    foreach (var item in varArrLst)
    {
        Console.WriteLine(item); ;
    }

}
//***********OUTPUT**********

1
90
1000

Using an AddRange not only means that we can add an array list but also just an array, sorted list, queue, and much more. Note that I have also given an example of how one can iterate over the array list.

Insert Element

Elements are inserted at a position, and a range can also be inserted.

For instance,

arrList.Insert(0, 2834);
int[] tens = { 10, 100, 1000, 10000 };
arrList.InsertRange(3, tens);

Properties of an Array List

In this paragraph, I will list out the various properties in arraylist that can be used at any time to get information about the array list.

Capacity Get or set the capacity or the max elements the array list can contain
CountGets the number of elements in the array list at any point
IsFixedSizeQueries if the array list has a fixed size or not
Item[index]Can set a value of the item in the array list with a particular index
Properties of an array list

Similarly, we have methods which can be used to do various operations on the elements of the array list. AddRange as discussed above is one such method. I will list a few of them that are frequently used.

Remove(object)Remove the first occurrence of the value in object from the array list
RemoveAt(index)Remove an element specified at an index
Reverse()Reverses the entire array list
Sort()Sort the elements in the array list
Sort(index1, index2, IComparator)Sorts a range of elements from index1 to index2 by using the comparator function passed
ToString()Returns a string representation of the object
Methods in array list

There are many more methods and if you would like to read through this please go to this for detailed reference.

Conclusion

I would like to conclude this tutorial by summarizing what all we went through. We learn about what is an array list, how to create one, how to add elements, how to insert at a position. After that, I gave a small note about some of the most used properties and methods of array list and a reference link for more details. Hoping you enjoyed this article.

Want to code and try for yourself? Click here

You can subscribe to telestreak for more updates and eBooks.

Download a cheatsheet for C#

What you get is an example for each concept in:

  • Variables
  • operators
  • OOP basics
  • collections
  • conditionals
  • best practices
    Glance through and refresh an example of all the topics listed above.
    Click subscribe and download your copy of the cheat sheet.

Go back to lesson overview

Prev: Collections

Next: Coming soon