ArrayList in C#: A tutorial for beginners

ArrayList in C#An ArrayList is a sophisticated version of the primitive array type. With a normal array, once you set the array size, you can’t change it. With an ArrayList, you can add and remove elements and the ArrayList variable will automatically resize itself accordingly. Also, an ArrayList gives you some additional methods and properties that aren’t available with a normal array.

Learn how to work with an ArrayList quickly and painlessly at Udemy.com

The ArrayList class is found in the System.Collections namespace in the .NET library. Instead of typing “System.Collections” with every variable declaration, you can add the namespace to the top of your C# code file. Add “using System.Collections;” to the top of your file, so now you only need to type “ArrayList” instead of “System.Collections.ArrayList” with every variable.

The first step when working with an ArrayList is to create an instance of the class. You create an instance using the following code:

ArrayList myarraylist = new ArrayList();

After you have a class instance, you can start adding elements to the ArrayList. You can add any data type to the list, but in this example, we will add integer values. The following code adds two integers to the myarraylist variable:

myarraylist.Add(1);
myarraylist.Add(2);

You can determine the number of elements in your ArrayList using the “Count()” method. In other words, “myarraylist.Count()” would return 2 in this example.

The next step is to run your application and add a breakpoint on your ArrayList. Set a breakpoint on the class instance. When the code stops on your breakpoint, drag and drop the myarraylist variable to your watch list.

Hover your mouse over the myarraylist variable before the “Add” function runs. Notice it’s null before you create a class instance. After the class instance statement runs, your variable is given an ArrayList data type but it contains no elements. After you run each Add() statement, the ArrayList automatically increases its size.

Stop your debugger. You can also remove elements from the ArrayList. You can remove elements at a specific index or you can remove an element by value. For instance, if you use “myarraylist.RemoveAt(1)” then the second element is removed (remember array indexes start at number 0). If you use “myarraylist.Remove(1)” then you actually delete the element with the value 1, and in this case, it’s the first element in the array.

Start the program again with the same breakpoint. As you step through your code, watch what happens to the ArrayList. It grows to two values and then its size is reduced as the remove function is executed.

You’ll probably need to read values from an ArrayList during your code execution. You can also sort elements in your ArrayList using the Sort() function. You can sort them in descending order using the Reverse() function.

Understand how to work with an ArrayList and all its methods

Run your code again and step through the code. Notice the array is automatically sorted numerically after the Sort() function and then reversed when the Reverse() function is executed.

The ArrayList class implements the IEnumerable interface. Because of this, you can also loop through values using the “foreach” loop. The following code is an example of a foreach loop with the ArrayList:

foreach (var num in myarraylist)
{
Console.WriteLine(num);
}

Finally, you can remove all elements in an ArrayList using the “Clear()” function. You can also view all of these statements in the debugger. Run your program again and step through your code. Notice you originally have an ArrayList that has added values. Then, you step through those values and then all values are cleared.

Knowing how to use the ArrayList object makes working with arrays much easier, and you avoid much of the hassle with normal arrays.

For a recap or to get into more C# functions, join hundreds of other students on this course on C for beginners.