top of page

C# Tutorial For Beginners: Working with collections(ArrayList)

Updated: Aug 22, 2021

The namespace “System. Collections” contains different types of collections that we can use in our program instead of using simple arrays. In this article, I will cover the ‘Array List’ class which allows us to create a ‘Dynamic’ array that can store different types and objects and variables in a single collection. For example:


static void Main(string[] args)

{

string a = "Hallo!!";

int b = 4;

ArrayList FirstExample = new ArrayList() { a,b};

}


Like a simple array, collections have different methods and properties that help us controlling the collection values and size, let’s review the main methods.


Adding a new item to an array


class Program

{

static void Main(string[] args)

{

//Adding items to a collection

int counter = 0;

ArrayList ArrayCollection = new ArrayList() {"String A", "String B"};

ArrayCollection.Add("String C");

ArrayCollection.Add("String D");

foreach (var item in ArrayCollection)

{

Console.WriteLine("Cell Number is: {0} | Value in cell: {1}", counter, item);

counter++;

}

}

}


Result


Removing an item from a collection

we can use different methods in order to remove items from a collection, lets examine the different options we have.


static void Main(string[] args)

{

int counter = 0;

ArrayList ArrayCollection = new ArrayList()

{"String A", "String B", "String C" , "String D", "String E", "String F" };


//Option 1: Remove a specific value

ArrayCollection.Remove("String C");


//Option 2: Remove a value base on a specific index

ArrayCollection.RemoveAt(0); //Will remove "String A"


//Option 3: Remove a specific range

ArrayCollection.RemoveRange(3,1); //Will remove "String F"


foreach (var item in ArrayCollection)

{

Console.WriteLine("Cell Number is: {0} | Value in cell: {1}", counter, item);

counter++;

}

}


Result


The ‘’Count’ and ‘Capacity’ methods

The ‘Count’ method returns the actual numbers of initialized indexes that reside in the collection and the ‘Capacity’ method returns the collection size (initialized and not initialized indexes).


class Program

{

static void Main(string[] args)

{

int counter = 0;

ArrayList ArrayCollection = new ArrayList()

{"String A", "String B", "String C" , "String D", "String E", "String F" };


Console.WriteLine(ArrayCollection.Count);// Will Return the acutal values in the array (6)

Console.WriteLine(ArrayCollection.Capacity);//Will return the number of elements the array can contain(8)

}

}


The ‘’Clear” method

This method clear all elements in a collection


static void Main(string[] args)

{

ArrayList ClearMethod = new ArrayList() { 'A', 'B', 'C', 'D', 'E' };

Console.WriteLine("Elements in collection: " + ClearMethod.Count);

ClearMethod.Clear(); //Remove all objects

Console.WriteLine("Elements in collection (Post Remove): " + ClearMethod.Count);

}


Result


The ‘Contains’ method

This method is used to search for a specific object name in the collections and will return a Boolean value (True or False).


static void Main(string[] args)

{

ArrayList ContainsMethod = new ArrayList() { 'A', 'B', 'C' };

Console.WriteLine("Collection contain object A ?\nResult:" + ContainsMethod.Contains('A'));

Console.WriteLine("Collection contain object D ?\nResult:" + ContainsMethod.Contains('D'));

}


Result


The ‘IndexOf’ method

The use of this method is done when we need to search for the index of a specific element in an array.


static void Main(string[] args)

{

ArrayList ContainsMethod = new ArrayList() { 'A', 'B', 'C' };

Console.WriteLine("Index Value: " + ContainsMethod.IndexOf('C'));//Will Return the value of '2'

Console.WriteLine("Index Value: " + ContainsMethod.IndexOf('4'));//Will Return -1 as the value not in collection

}