C# ARRAYLIST

In C#, the ArrayList is a non-generic collection of objects whose size increases dynamically. It is the same as Array except that its size increases dynamically.
An ArrayList can be used to add unknown data where you don’t know the types and the size of the data.
NB: An ArrayList can contain multiple null and duplicate values.
Create an ArrayList
using System.Collections;
ArrayList arlist = new ArrayList();
// or
var arlist = new ArrayList(); // recommended
ArrayList Methods:
- Add() to add elements to an ArrayList
- AddRange(Icollection c) to add an entire Array, HashTable, SortedList, ArrayList, BitArray, Queue, and Stack in the ArrayList.
- Insert () to insert an element at the specified index into an ArrayList.
- InsertRange() to insert a collection in an ArrayList at the specfied index.
- Remove(), RemoveAt(), or RemoveRange methods to remove elements from an ArrayList.
- Contains() method to determine whether the specified element exists in the ArrayList or not. It returns true if exists otherwise returns false.
Accessing an ArrayList
The ArrayList class implements the IList interface. So, elements can be accessed using indexer, in the same way as an array. Index starts from zero and increases by one for each subsequent element.
Example:
var arlist = new ArrayList() { 1, “Bill”, 300, 4.5f };
//Access individual item using indexer
int firstElement = (int) arlist[0];
returns 1
string secondElement = (string) arlist[1];
returns “Bill“
//update elements
arlist[0] = “Steve”;
arlist[1] = 100;
arlist[5] = 500; //Error: Index out of range
Iterate an ArrayList:
The ArrayList implements the ICollection interface that supports iteration of the collection types. So, use the foreach and the for loop to iterate an ArrayList. The Count property of an ArrayList returns the total number of elements in an ArrayList.
ArrayList arlist = new ArrayList() { 1, “Bill”, 300, 4.5F };
foreach (var item in arlist)
Console.Write(item + “, “);
//output: 1, Bill, 300, 4.5,
for(int i = 0 ; i < arlist.Count; i++)
Console.Write(arlist[i] + “, “);
//output: 1, Bill, 300, 4.5,