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.
Filter by Category
Array lists are one of the simplest varieties of lists that C# libraries have to offer and this tutorial explains how to use them.
Quicksort algorithm or divide and conquer algorithm is one of the most used sorting algorithms, because of its supperior efficiency.
Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.
Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.
Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.
C# arrays are data structures for storing multiple items of the same type. This guide shows you how to use different varieties of arrays.
SLIC superpixel segmentation is a modern operation for reducing irrelevant detail for shortening computational time in further processing.
K means clustering is a optimization method of partitioning an image by measuring Euclidean distances between pixels and cluster means.
Region splitting and merging is a texture segmentation operation, where we use descriptors such as local mean intensity and standard deviation
Region growing segmentation is a process, with which we can extract regions from image based on the properties of pixels inside them.
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.
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();
arraylist.Add(9);
//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));
arraylist.Remove("Hello");
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.