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, ​

--

--

--

Web designer/developer, cyber security and poet.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creating an ammo refill powerup

FCM Integration for Cordova Hybrid Apps.

Openpilot Stop and Go Hack for Thailand Toyota Corolla Cross Hybrid Premium Safety model with comma…

THIS WEEK ON AI CLASS

Rendering MJML in PHP

Mega Mod Apk Unlimited Storage 2022

Google Chrome — From being David to becoming Goliath in a monopoly browser market

Getting Started With Action On Google — Part 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Seun Daniel Omatsola

Seun Daniel Omatsola

Web designer/developer, cyber security and poet.

More from Medium

C# “ENCAPSULATION,PROPERTY AND PROP”

Garbage Collection in .NET

C# Understanding Interfaces + simple mocking + testing (xUnit + NSubstitute)

Async programming vs Parallel programming (Basic)