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
Getting an enumerator for a range of elements in the ArrayList in C#
To get an enumerator for a range of elements in the ArrayList, you can use the GetEnumerator(int, int) method. This method allows you to iterate through a specific subset of elements starting from a given index with a specified count.
Syntax
Following is the syntax for getting an enumerator for a range of elements −
IEnumerator enumerator = arrayList.GetEnumerator(startIndex, count);
Parameters
- startIndex − The zero-based starting index of the range.
- count − The number of elements in the range.
Return Value
Returns an IEnumerator object that can iterate through the specified range of elements in the ArrayList.
Using GetEnumerator with Numeric Values
Example
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList arrList = new ArrayList();
arrList.Add(100);
arrList.Add(200);
arrList.Add(300);
arrList.Add(400);
arrList.Add(500);
Console.WriteLine("Display elements in a range...");
IEnumerator demoEnum = arrList.GetEnumerator(1, 3);
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
}
}
The output of the above code is −
Display elements in a range... 200 300 400
Using GetEnumerator with Multiple Ranges
Example
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList arrList = new ArrayList();
arrList.Add("Andy");
arrList.Add("Katie");
arrList.Add("Taylor");
arrList.Add("Steve");
arrList.Add("Nathan");
arrList.Add("Carl");
arrList.Add("Mark");
arrList.Add("Amy");
arrList.Add("Jacob");
arrList.Add("John");
Console.WriteLine("Display elements in a range...");
IEnumerator demoEnum = arrList.GetEnumerator(1, 3);
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
Console.WriteLine("Display elements in a range...");
demoEnum = arrList.GetEnumerator(3, 5);
while (demoEnum.MoveNext()) {
Object ob = demoEnum.Current;
Console.WriteLine(ob);
}
}
}
The output of the above code is −
Display elements in a range... Katie Taylor Steve Display elements in a range... Steve Nathan Carl Mark Amy
How It Works
The GetEnumerator(int, int) method creates an enumerator that starts at the specified startIndex and iterates through exactly count number of elements. The enumerator follows the standard pattern using MoveNext() to advance and Current to access the element at the current position.
Common Use Cases
- Pagination − Display a subset of data for pagination purposes.
- Performance − Process only a specific range to improve performance.
- Data Validation − Check a specific range of elements without iterating through the entire collection.
Conclusion
The GetEnumerator(int, int) method in ArrayList provides efficient access to a specific range of elements. It accepts a starting index and count, returning an enumerator that iterates only through the specified subset, which is useful for scenarios requiring partial collection traversal.
