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
Gets or Sets the element at the specified index in the List in C#
In C#, you can get or set elements at a specific index in a List<T> using the indexer property. This allows you to access and modify list elements using square bracket notation, similar to arrays.
Syntax
Following is the syntax for accessing elements by index −
// Getting an element T element = list[index]; // Setting an element list[index] = value;
Getting Elements by Index
The following example demonstrates how to retrieve elements from a List<string> using their index positions −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
List<string> list = new List<string>();
list.Add("100");
list.Add("200");
list.Add("300");
list.Add("400");
list.Add("500");
list.Add("600");
list.Add("700");
list.Add("800");
list.Add("900");
list.Add("1000");
Console.WriteLine("Element at index 0 = " + list[0]);
Console.WriteLine("Element at index 1 = " + list[1]);
Console.WriteLine("Element at index 2 = " + list[2]);
Console.WriteLine("Element at index 3 = " + list[3]);
Console.WriteLine("Element at index 4 = " + list[4]);
Console.WriteLine("Element at index 5 = " + list[5]);
Console.WriteLine("Element at index 6 = " + list[6]);
Console.WriteLine("Element at index 7 = " + list[7]);
}
}
The output of the above code is −
Element at index 0 = 100 Element at index 1 = 200 Element at index 2 = 300 Element at index 3 = 400 Element at index 4 = 500 Element at index 5 = 600 Element at index 6 = 700 Element at index 7 = 800
Setting Elements by Index
You can also modify existing elements by assigning new values to specific index positions −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
List<string> list = new List<string>();
list.Add("100");
list.Add("200");
list.Add("300");
list.Add("400");
list.Add("500");
Console.WriteLine("Original element at index 3 = " + list[3]);
// Setting a new value at index 3
list[3] = "450";
Console.WriteLine("Updated element at index 3 = " + list[3]);
// Multiple updates
list[0] = "150";
list[4] = "550";
Console.WriteLine("After multiple updates:");
for (int i = 0; i < list.Count; i++) {
Console.WriteLine("Element at index " + i + " = " + list[i]);
}
}
}
The output of the above code is −
Original element at index 3 = 400 Updated element at index 3 = 450 After multiple updates: Element at index 0 = 150 Element at index 1 = 200 Element at index 2 = 300 Element at index 3 = 450 Element at index 4 = 550
Index Bounds and Error Handling
It's important to ensure that the index is within the valid range (0 to Count - 1). Accessing an invalid index throws an ArgumentOutOfRangeException −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(string[] args) {
List<int> numbers = new List<int> { 10, 20, 30 };
Console.WriteLine("List count: " + numbers.Count);
Console.WriteLine("Valid index range: 0 to " + (numbers.Count - 1));
// Safe access
if (2 < numbers.Count) {
Console.WriteLine("Element at index 2: " + numbers[2]);
}
// Demonstrate bounds checking
try {
Console.WriteLine("Attempting to access index 5...");
int value = numbers[5]; // This will throw an exception
}
catch (ArgumentOutOfRangeException ex) {
Console.WriteLine("Error: " + ex.Message.Split('.')[0]);
}
}
}
The output of the above code is −
List count: 3 Valid index range: 0 to 2 Element at index 2: 30 Attempting to access index 5... Error: Index was out of range
Conclusion
The indexer property in List<T> provides a convenient way to get and set elements using square bracket notation. Always ensure the index is within the valid range (0 to Count-1) to avoid runtime exceptions, and consider using bounds checking for safer code.
