Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Get or set the element at the specified index in ArrayList in C#
The ArrayList class in C# provides an indexer property that allows you to get or set elements at a specified index using square bracket notation. This property provides direct access to elements by their position in the collection.
Syntax
Following is the syntax for accessing elements by index in ArrayList −
// Get element at index object element = arrayList[index]; // Set element at index arrayList[index] = value;
Parameters
index − The zero-based index of the element to get or set.
value − The object to store at the specified index when setting.
Return Value
The indexer returns an object representing the element at the specified index. When setting, it stores the provided value at that position.
Getting Elements from ArrayList
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("Laptop");
arrList.Add("Desktop");
arrList.Add("Notebook");
arrList.Add("Ultrabook");
arrList.Add("Tablet");
arrList.Add("Headphone");
arrList.Add("Speaker");
Console.WriteLine("Elements in ArrayList...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 5 = " + arrList[5]);
Console.WriteLine("Element at index 0 = " + arrList[0]);
Console.WriteLine("Element at index 3 = " + arrList[3]);
}
}
The output of the above code is −
Elements in ArrayList... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 5 = Headphone Element at index 0 = Laptop Element at index 3 = Ultrabook
Setting Elements in ArrayList
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("Laptop");
arrList.Add("Desktop");
arrList.Add("Notebook");
arrList.Add("Ultrabook");
arrList.Add("Tablet");
arrList.Add("Headphone");
arrList.Add("Speaker");
Console.WriteLine("Original ArrayList...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 5 = " + arrList[5]);
// Set new value at index 5
arrList[5] = "SSD";
arrList[2] = "Gaming Laptop";
Console.WriteLine("\nUpdated ArrayList...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("Element at index 5 (Updated) = " + arrList[5]);
Console.WriteLine("Element at index 2 (Updated) = " + arrList[2]);
}
}
The output of the above code is −
Original ArrayList... Laptop Desktop Notebook Ultrabook Tablet Headphone Speaker Element at index 5 = Headphone Updated ArrayList... Laptop Desktop Gaming Laptop Ultrabook Tablet SSD Speaker Element at index 5 (Updated) = SSD Element at index 2 (Updated) = Gaming Laptop
Key Points
ArrayList uses zero-based indexing, where the first element is at index 0.
Accessing an invalid index throws an
ArgumentOutOfRangeException.The indexer returns
objecttype, so casting may be required for specific types.Setting an element at an index replaces the existing value at that position.
Conclusion
The ArrayList indexer provides convenient access to elements using square bracket notation. You can both retrieve and modify elements at specific positions, making it easy to work with ArrayList collections when you know the index of the elements you need to access.
