C# में Stack.Peek() मेथड स्टैक के शीर्ष (top) पर स्थित ऑब्जेक्ट को बिना हटाए लौटाता है। यह आपको स्टैक को संशोधित किए बिना शीर्ष एलिमेंट को देखने की अनुमति देता है।
सिंटैक्स
public virtual object Peek ();
उदाहरण 1: Peek के साथ स्टैक ऑपरेशंस
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 at the top = " + stack.Peek());
stack.Push("Ultrabook");
stack.Push("Cameras");
stack.Push("Keyboards");
Console.WriteLine("\nStack elements...updated");
foreach(string val in stack) {
Console.WriteLine(val);
}
Console.WriteLine("Element at the top = " + stack.Peek());
Console.WriteLine("\nCount of elements (updated) = " + stack.Count);
stack.Clear();
Console.Write("Count of elements (after Clear) = " + stack.Count);
}
}
आउटपुट
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element at the top = Notebook Stack elements...updated Keyboards Cameras Ultrabook Notebook Laptop XPS Monitors Projectors Alienware Inspiron Element at the top = Keyboards Count of elements (updated) = 10 Count of elements (after Clear) = 0
उदाहरण 2: Contains, Clone और Peek का संयुक्त उपयोग
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 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("Element Alienware in stack? = " + stack.Contains("Alienware"));
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nStack elements...cloned");
foreach(string val in stack2) {
Console.WriteLine(val);
}
Console.Write("Count of elements (stack2) = " + stack2.Count);
Console.WriteLine("Top of the Stack = " + stack2.Peek());
}
}
आउटपुट
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Element at the top = Notebook Stack elements...updated Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements (updated) = 10 Element Alienware in stack? = True Stack elements...cloned Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements (stack2) = 10 Top of the Stack = Earphone
Peek vs Pop तुलना
| विशेषता | Peek() | Pop() |
|---|---|---|
| शीर्ष एलिमेंट लौटाता है | हां | हां |
| स्टैक से हटाता है | नहीं | हां |
| खाली स्टैक पर अपवाद | हां (InvalidOperationException) | हां (InvalidOperationException) |
| Count बदलता है | नहीं | हां (घटाता है) |
सर्वोत्तम अभ्यास
- हमेशा
Peek()याPop()कॉल करने से पहलेif (stack.Count > 0)जांचें। - जेनेरिक
Stack<T>का उपयोग टाइप सेफ्टी के लिए करें।