- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 the entire ArrayList in C#?
To get an enumerator for the entire ArrayList in C#, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list1 = new ArrayList(); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); list1.Add("E"); list1.Add("F"); list1.Add("G"); list1.Add("H"); list1.Add("I"); Console.WriteLine("Elements in ArrayList1..."); foreach (string res in list1) { Console.WriteLine(res); } ArrayList list2 = new ArrayList(); list2.Add("A"); list2.Add("B"); list2.Add("C"); list2.Add("D"); list2.Add("E"); list2.Add("F"); list2.Add("G"); list2.Add("H"); list2.Add("I"); Console.WriteLine("Elements in ArrayList2..."); foreach (string res in list2) { Console.WriteLine(res); } Console.WriteLine("Count of elements in ArrayList2 = " + list2.Count); list2.RemoveAt(5); Console.WriteLine("Count of elements in ArrayList2 (Updated) = " + list2.Count); Console.WriteLine("Enumerator iterating the ArrayList2..."); IEnumerator demoEnum = list2.GetEnumerator(); while (demoEnum.MoveNext()) { Console.WriteLine(demoEnum.Current); } } }
Output
This will produce the following output −
Elements in ArrayList1... A B C D E F G H I Elements in ArrayList2... A B C D E F G H I Count of elements in ArrayList2 = 9 Count of elements in ArrayList2 (Updated) = 8 Enumerator iterating the ArrayList2... A B C D E G H I
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(100); arrList.Add(200); arrList.Add(300); arrList.Add(400); arrList.Add(500); Console.WriteLine("Enumerator iterating the ArrayList..."); IEnumerator demoEnum = arrList.GetEnumerator(); while (demoEnum.MoveNext()) { Console.WriteLine(demoEnum.Current); } } }
Output
This will produce the following output −
numerator iterating the ArrayList... 100 200 300 400 500
Advertisements