C# में Stack.GetEnumerator() मेथड का उपयोग Stack के लिए एक IEnumerator वापस करने के लिए किया जाता है।
सिंटैक्स
सिंटैक्स निम्नलिखित है:
public virtual System.Collections.IEnumerator GetEnumerator ();
उदाहरण
आइए अब एक उदाहरण देखते हैं:
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 तत्व...");
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 तत्व...");
while (demoEnum.MoveNext()) {
Console.WriteLine(demoEnum.Current);
}
Console.WriteLine("\nक्या दोनों स्टैक समान हैं? = " + stack1.Equals(stack2));
}
}
आउटपुट
यह निम्नलिखित आउटपुट देगा:
Stack1 तत्व... 1000 750 500 300 150 Stack2 तत्व... 900 850 500 400 350 क्या दोनों स्टैक समान हैं? = False
उदाहरण
आइए अब एक और उदाहरण देखते हैं:
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("स्टैक तत्व...");
foreach(string val in stack){
Console.WriteLine(val);
}
Console.WriteLine("तत्वों की संख्या = " + stack.Count);
Console.WriteLine("क्या 'Speakers' स्टैक में है? = " + stack.Contains("Speakers"));
stack.Push("Headphone");
stack.Push("Keyboard");
stack.Push("Earphone");
Console.WriteLine("\nस्टैक तत्व... अपडेटेड");
foreach(string val in stack){
Console.WriteLine(val);
}
Console.WriteLine("तत्वों की संख्या (अपडेटेड) = " + stack.Count);
Console.WriteLine("\nक्या 'Alienware' स्टैक में है? = " + stack.Contains("Alienware"));
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nस्टैक तत्व... क्लोन किया गया");
IEnumerator demoEnum = stack2.GetEnumerator();
while (demoEnum.MoveNext()) {
Console.WriteLine(demoEnum.Current);
}
Console.WriteLine("तत्वों की संख्या (अपडेटेड) = " + stack.Count);
}
}
आउटपुट
यह निम्नलिखित आउटपुट देगा:
स्टैक तत्व... Notebook Laptop XPS Monitors Projectors Alienware Inspiron तत्वों की संख्या = 7 क्या 'Speakers' स्टैक में है? = False स्टैक तत्व... अपडेटेड Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron तत्वों की संख्या (अपडेटेड) = 10 क्या 'Alienware' स्टैक में है? = True स्टैक तत्व... क्लोन किया गया Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron तत्वों की संख्या (अपडेटेड) = 10