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
Stack.GetEnumerator() Method in C#
The Stack.GetEnumerator() method in C# returns an IEnumerator that allows you to iterate through the elements of a Stack collection. This method provides a way to manually control the iteration process using the enumerator pattern, as an alternative to using foreach loops.
Syntax
Following is the syntax for the GetEnumerator() method −
public virtual System.Collections.IEnumerator GetEnumerator();
Return Value
The method returns an IEnumerator object that can be used to iterate through the Stack elements. The enumerator starts before the first element and advances using MoveNext(), with the current element accessed via the Current property.
Using GetEnumerator() with Manual Iteration
The following example demonstrates how to use GetEnumerator() to manually iterate through Stack elements −
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack1 = new Stack();
stack1.Push(150);
stack1.Push(300);
stack1.Push(500);
stack1.Push(750);
stack1.Push(1000);
Console.WriteLine("Stack1 elements using foreach...");
foreach(int val in stack1) {
Console.WriteLine(val);
}
Stack stack2 = new Stack();
stack2.Push(350);
stack2.Push(400);
stack2.Push(500);
stack2.Push(850);
stack2.Push(900);
IEnumerator demoEnum = stack2.GetEnumerator();
Console.WriteLine("Stack2 elements using GetEnumerator()...");
while (demoEnum.MoveNext()) {
Console.WriteLine(demoEnum.Current);
}
Console.WriteLine("\nAre both the stacks equal? = " + stack1.Equals(stack2));
}
}
The output of the above code is −
Stack1 elements using foreach... 1000 750 500 300 150 Stack2 elements using GetEnumerator()... 900 850 500 400 350 Are both the stacks equal? = False
Using GetEnumerator() with String Stack
The following example shows GetEnumerator() working with a Stack containing string values −
using System;
using System.Collections;
public class Demo {
public static void Main() {
Stack stack = new Stack();
stack.Push("Inspiron");
stack.Push("Alienware");
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Stack elements...");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements = " + stack.Count);
Console.WriteLine("Element Speakers in the stack? = " + stack.Contains("Speakers"));
stack.Push("Headphone");
stack.Push("Keyboard");
stack.Push("Earphone");
Console.WriteLine("\nStack elements...updated");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements (updated) = " + stack.Count);
Console.WriteLine("\nElement Alienware in the stack? = " + stack.Contains("Alienware"));
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nStack elements...cloned using GetEnumerator()");
IEnumerator demoEnum = stack2.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine(demoEnum.Current);
}
Console.WriteLine("Count of cloned stack elements = " + stack2.Count);
}
}
The output of the above code is −
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element Speakers in the stack? = False Stack elements...updated Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements (updated) = 10 Element Alienware in the stack? = True Stack elements...cloned using GetEnumerator() Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of cloned stack elements = 10
Key Points
-
The
GetEnumerator()method returns elements in LIFO (Last-In-First-Out) order, same as Stack's natural ordering. -
The enumerator is positioned before the first element initially. Call
MoveNext()to advance to the first element. -
Use
Currentproperty to access the current element after callingMoveNext(). -
The enumerator becomes invalid if the Stack is modified during iteration.
Conclusion
The Stack.GetEnumerator() method provides manual control over Stack iteration using the IEnumerator pattern. While foreach loops are more convenient, GetEnumerator() offers fine-grained control when you need to implement custom iteration logic or handle iteration states explicitly.
