Related Articles

Back to Latest Articles

How To Make Array Lists In C# – Made Easy

Array lists are one of the simplest varieties of lists that C# libraries have to offer and this tutorial explains how to use them.

Andraz Krzisnik
How To Make Array Lists In C# – Made...

Array lists in C# are a type of data structures that allow us to store objects inside, much like arrays. However, we can’t change the size of arrays once we already declared them, while we can do this with lists. Therefore, if we’re working with larger datasets, they would be a better choice for that application.

These type of lists are just one variety of many. However, I’m going to focus only on array lists in this post, because of two reasons. Firstly, this post will serve as an introduction to lists in C#. And secondly, we can say this list type is one of the simplest and is perfect for a starting point.

In C#, we can declare array lists by using ArrayList class from System.Collections namespace. Furthermore, this class gives us many methods we can choose from to work with our ArrayList object. To list a few of these, we can add and remove, which are essential for working with lists.

Not only these, but we can also get properties from them like how many objects we’re storing inside, what’s its capacity, what’s the index of a certain value and so on. We’ll get more into these operations once we get to implement them in code.

Using array lists in C#

So, without further ado, let’s dive into a few examples of these operations we spoke of in the preceding paragraph.

ArrayList arraylist = new ArrayList();

//How to transform array list to array
object[] array = arraylist.ToArray();
Console.WriteLine(string.Join(',', array));

arraylist.AddRange(new int[] { 8, -4, 22 });

array = arraylist.ToArray();
Console.WriteLine(string.Join(',', array));

arraylist.AddRange(new string[] { "Hello", "Hello", "World" });

array = arraylist.ToArray();
Console.WriteLine(string.Join(',', array));


array = arraylist.ToArray();
Console.WriteLine(string.Join(',', array));

object find = arraylist.IndexOf(22);
Console.WriteLine("Index of number 22: " + find);

In the lines of code above, as you can see, we used the methods we spoke of earlier, but there is also one we didn’t. I’m talking about AddRange method, which we can use to add multiple objects to our list at once.

And as a bonus, I also added an example on how to convert an array list into an array, which in our case proved to be useful for displaying values inside the list.

We also mentioned that you can get the certain properties like Count and Capacity. To clarify, Count will return the number of objects that are already stored. While on the other hand, Capacity returns the number of how many objects can be stored. The following block of code shows how you can do this.

int count = arraylist.Count;
Console.WriteLine("Number of objects stored: " + count);

int capacity = arraylist.Capacity;
Console.WriteLine("How many objects can be stored: " + capacity);


I hope this short tutorial helped you gain a better understanding on how array lists work and what we can do with them.

In case you want to see how it all works in practice, you can download a demo project I put together and try it out yourself.

Related Articles

Digital Image Watermarking

How To Make Image Watermarking Work With C#

This tutorial shows how image watermarking works by implementing it with C# and we describe various purposes that watermarks have as well.

Posted on by Andraz Krzisnik
Order-Statistic Filters

How To Make Alpha Trimmed Mean Filter For Images

Alpha trimmed mean filter is a combination of mean filters and order-statistic filters. This guide shows how to use them with C#.

Posted on by Andraz Krzisnik