Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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, the code is as follows −
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);
}
}
}
Output
This will produce the following output −
Display elements in a range... 200 300 400
Example
Let us now see another 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);
}
}
}
Output
This will produce the following output −
Display elements in a range... Katie Taylor Steve Display elements in a range... Steve Nathan Carl Mark Amy
Advertisements