In c#, ArrayList is useful to store elements of different data types and the size of ArrayList can grow or shrink dynamically based on the need of our application like adding or removing elements from ArrayList.
In c#, arraylists are same as arrays but the only difference is arrays are used to store a fixed number of same data type elements.
In c#, ArrayList is a non-generic type of collection and it is provided by System.Collections namespace.
As discussed, the collection is a class so to define an arraylist, you just need to declare an instance of the arraylist class before we perform any operations like add, delete, etc. like as shown below.
ArrayList arrList = new ArrayList();
If you observe the above arraylist declaration, we created a new arraylist (arrList) with an instance of arraylist class without specifying any size.
The following are the some of commonly used properties of an arraylist in c# programming language.
Property | Description |
---|---|
Capacity | It is useful to get or set the number of elements an arraylist can contain. |
Count | It is useful to get the number of elements in an arraylist. |
IsFixedSize | It is useful to get a value to indicate that the arraylist has a fixed size or not. |
IsReadOnly | It is useful to get a value to indicate that the arraylist is read-only or not. |
Item | It is used to get or set an element at the specified index. |
IsSynchronized | It is used to get a value to indicate that access to arraylist is synchronized (thread-safe) or not. |
The following are some of the commonly used methods of an arraylist to add, search, insert, delete or sort elements of arraylist in c# programming language.
Method | Description |
---|---|
Add | It is useful to add an element at the end of arraylist. |
AddRange | It is useful to add all the elements of the specified collection at the end of arraylist. |
Clear | It will remove all the elements in arraylist. |
Clone | It will create a shallow copy of arraylist. |
Contains | It is useful to determine whether the specified element exists in an arraylist or not. |
CopyTo | It is useful to copy all the elements of an arraylist into another compatible array. |
GetRange | It is useful to return a specified range of arraylist elements starting from the specified index. |
IndexOf | It is useful to search for a specified element and return a zero-based index if found otherwise return -1 if no element found. |
Insert | It is useful to insert an element in arraylist at the specified index. |
InsertRange | It is useful to insert all the elements of the specified collection into arraylist starting from the specified index. |
Remove | It is useful to remove the first occurrence of specified element from the arraylist. |
RemoveAt | It is useful to remove an element from arraylist based on the specified index position. |
RemoveRange | It is useful to remove a range of elements from an arraylist. |
Reverse | It reverses the order of arraylist elements. |
Sort | It sorts the elements in an arraylist. |
ToArray | It copies all the elements of an arraylist to a new array object. |
Following is the example of using an arraylist in c# programming language.
using System;
using System.Collections;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5);
arrlist.Add("Tutlane");
Console.WriteLine("ArrayList Count: " + arrlist.Count);
Console.WriteLine("ArrayList Capacity: " + arrlist.Capacity);
Console.WriteLine("*********ArrayList Elements********");
foreach (var item in arrlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
If you observe above example, we are able to define a new arraylist (arrlist) collection by using System.Collections namespace. Here, we are binding a different data type values to newly created arraylist (arrlist) by using Add method and with Count & Capacity properties we are able to access number of elements in an arraylist.
When you execute the above c# program, you will get the result as shown below.
This is how you can use arraylist in c# to hold a group of different data type objects based on our requirements.
The following are some of the useful examples to work with arraylist in c# programming language.
In c#, by using the Add or AddRange method, we can add a different data type elements to an arraylist.
Following is the example of adding elements to arraylist using Add or AddRange methods in c#.
using System;
using System.Collections;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
// creating arraylist
ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5);
arrlist.Add("Tutlane");
//creating arraylist
ArrayList arrlist2 = new ArrayList() { 10, "Hi" };
// adding arrlist2 to arrlist
arrlist.AddRange(arrlist2);
Console.WriteLine("*********ArrayList Elements********");
foreach (var item in arrlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
If you observe above example, we created a multiple arraylists (arrlist, arrlist2) and adding an elements to the arraylists in different ways.
When you execute the above c# program, you will get the result as shown below.
In c#, you have different ways to access arraylist elements i.e. either by using index positions or by iterating through an arraylist using for / foreach loops.
Following is the example of accessing arraylist elements in different ways.
using System;
using System.Collections;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
// creating arraylist
ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5f);
arrlist.Add("Tutlane");
// accessing first element
string msg = (string)arrlist[0];
// accessing third element
float num = (float)arrlist[2];
Console.WriteLine("*********Access Elements with Index Position********");
Console.WriteLine("Element at 0: " + msg);
Console.WriteLine("Element at 2: " + num);
Console.WriteLine("*********Access Elements with For Loop********");
// for loop to access arraylist elements
for (int i = 0; i < arrlist.Count; i++)
{
Console.WriteLine(arrlist[i]);
}
Console.WriteLine("*********Access Elements with Foreach Loop********");
// foreach loop to access arraylist elements
foreach (var item in arrlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
If you observe above example, we are accessing arraylist elements in different ways by using index positions, for and foreach loops based on our requirements.
When you execute the above c# program, you will get the result like as shown below.
In c#, we can insert elements into arraylist either by using Insert() or InsertRange() methods.
Following is the example of inserting elements into an arraylist by using Insert and InsertRange methods in c#.
using System;
using System.Collections;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
// creating arraylist
ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5f);
arrlist.Add("Tutlane");
// inserting elements into arraylist
arrlist.Insert(0, "Hi");
arrlist.Insert(1, 50);
ArrayList arrlist2 = new ArrayList() { 200, 300 };
// inserting arrlist2 into arrlist at position 2
arrlist.InsertRange(2, arrlist2);
Console.WriteLine("*********ArrayList Elements********");
foreach (var item in arrlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
If you observe above example, we inserted an elements into arraylist at different index positions (0, 1) by using Insert() method. Same way, by using InsertRange() method we are inserting all the element of newly created arraylist (arrlist2) into arrlist at index position 2.
When you execute the above c# program, you will get the result as shown below.
In c#, you have a different methods to remove an elements from arraylist i.e. either by using Remove() or RemoveAt() or RemoveRange() methods.
Following is the example of deleting elements from arraylist in c# programming language.
using System;
using System.Collections;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
// creating arraylist
ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5f);
arrlist.Add("Tutlane");
arrlist.Add(200);
arrlist.Add(300);
arrlist.Add(400);
// Removing an element which is having a value 20.5f
arrlist.Remove(20.5f);
// Removing an element at index 0
arrlist.RemoveAt(0);
// Removing 2 elements starting from index 3
arrlist.RemoveRange(3, 2);
Console.WriteLine("*********ArrayList Elements********");
foreach (var item in arrlist)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
If you observe above example, we used a Remove() method to delete particular value of element from arraylist and used a RemoveAt() method to delete an element at specified index position.
Same way, we used the RemoveRange() method to delete the specified number of elements from arraylist starting from the specified index position.
When you execute the above c# program, you will get the result as shown below.
In case, if you want to remove all the elements from arraylist, then use the Clear() method.
Same way, we can use different methods like Sort() method to sort elements of arraylist and Reverse() method to reverse the order of arraylist elements.
By using Contains() method, we can check whether the specified element exists in arraylist or not. In case, if it exists it will return true otherwise false.
Following is the example of using Contains() method to check for an item that exists in arraylist or not in c#.
using System;
using System.Collections;
namespace Tutlane
{
class Program
{
static void Main(string[] args)
{
// creating arraylist
ArrayList arrlist = new ArrayList();
arrlist.Add("Welcome");
arrlist.Add(100);
arrlist.Add(20.5f);
arrlist.Add("Tutlane");
// check for an item 100 exists in arraylist or not
Console.WriteLine("Item Exists: " + arrlist.Contains(100));
Console.ReadLine();
}
}
}
If you observe above example, we used a Contains() method to check for an item (100) exists in arraylist (arrlist) or not.
When you execute the above c# program, you will get the result as shown below.
The following are the important points that need to remember about arraylist in c#.
The following are the main difference between array and arraylist in c# programming language.