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 an enumerator that iterates through StringCollection in C#
The StringCollection class in C# provides a GetEnumerator() method that returns a StringEnumerator object. This enumerator allows you to iterate through the collection elements one by one using the MoveNext() and Current properties.
Syntax
Following is the syntax for getting an enumerator from StringCollection −
StringEnumerator enumerator = stringCollection.GetEnumerator();
Following is the syntax for iterating using the enumerator −
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
Using GetEnumerator() with StringCollection
The GetEnumerator() method returns a StringEnumerator that provides MoveNext() and Current members for manual iteration −
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "300", "400", "500" };
Console.WriteLine("Array elements...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("Does the specified string is in the StringCollection? = " + stringCol.Contains("800"));
Console.WriteLine("Total number of elements = " + stringCol.Count);
Console.WriteLine("Iterating through StringCollection...");
StringEnumerator myenum = stringCol.GetEnumerator();
while (myenum.MoveNext())
Console.WriteLine(myenum.Current);
}
}
The output of the above code is −
Array elements... 100 200 300 400 500 Does the specified string is in the StringCollection? = False Total number of elements = 5 Iterating through StringCollection... 100 200 300 400 500
Using GetEnumerator() with Individual Additions
You can also use the enumerator when elements are added individually using the Add() method −
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
stringCol.Add("10");
stringCol.Add("20");
stringCol.Add("30");
stringCol.Add("40");
stringCol.Add("50");
stringCol.Add("60");
stringCol.Add("70");
stringCol.Add("80");
stringCol.Add("90");
stringCol.Add("100");
Console.WriteLine("Elements...");
foreach (string res in stringCol) {
Console.WriteLine(res);
}
Console.WriteLine("Iterating through StringCollection with Enumerator...");
StringEnumerator myenum = stringCol.GetEnumerator();
while (myenum.MoveNext())
Console.WriteLine(myenum.Current);
}
}
The output of the above code is −
Elements... 10 20 30 40 50 60 70 80 90 100 Iterating through StringCollection with Enumerator... 10 20 30 40 50 60 70 80 90 100
Comparison of Iteration Methods
| Method | Syntax | Use Case |
|---|---|---|
| foreach loop | foreach (string item in collection) |
Simple iteration, cleaner syntax |
| GetEnumerator() | while (enumerator.MoveNext()) |
Manual control over iteration |
Conclusion
The GetEnumerator() method in StringCollection returns a StringEnumerator that provides manual control over iteration using MoveNext() and Current. While foreach loops are more convenient, enumerators offer fine-grained control when needed for specific iteration requirements.
