Udemy logo

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.

Page Last Updated: April 2014

Top courses in C# (programming language)

Ultimate C# Masterclass for 2023
Krystyna Ślusarczyk
4.7 (973)
Bestseller
C# / .NET Interview Questions with Answers.
Shivprasad Koirala
4.7 (1,239)
Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (40,210)
Bestseller
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.5 (3,516)
Bestseller
Unity RPG Inventory Systems Asset Pack: Behind The Scenes
GameDev.tv Team, Rick Davidson
4.5 (836)
The Unity C# Survival Guide
Jonathan Weinberger, Unity Technologies, GameDevHQ Team
4.8 (1,727)
Advanced Topics in C#
Dmitri Nesteruk
4.5 (542)
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.8 (205)
Bestseller
Design Patterns in C# Made Simple
Zoran Horvat
4.8 (399)
Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.6 (27,144)

More C# (programming language) Courses

C# (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo