কম্পিউটার টিউটোরিয়াল

C#-এ Stack.Peek() মেথড


C#-এ Stack.Peek() মেথডটি Stack-এর উপরের (top) এলিমেন্টটি রিটার্ন করে কিন্তু এটি Stack থেকে সরিয়ে দেয় না। Stack LIFO (Last In, First Out) নীতিতে কাজ করে।

সিনট্যাক্স (Syntax)

public virtual object Peek ();

উদাহরণ ১: স্ট্রিং Stack-এ 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 (updated) = " + 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 (updated) = 0

উদাহরণ ২: 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 is 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("Element Alienware is the 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 (updated) = " + 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 
Keyboards 
Cameras 
Ultrabook 
Notebook 
Laptop 
XPS 
Monitors 
Projectors 
Alienware 
Inspiron 
Element at the top = Keyboards 
Count of elements (updated) = 10 
Count of elements (updated) = 0

মূল পয়েন্টস

  • Peek() মেথড Stack-এর টপ এলিমেন্টটি শুধু দেখে, সরিয়ে দেয় না।
  • যদি Stack খালি থাকে, Peek() কল করলে InvalidOperationException থ্রো করে।
  • Pop() মেথড টপ এলিমেন্টটি রিটার্ন করে এবং সেটিকে Stack থেকে সরিয়ে দেয়।
  • পূর্বে Clear() কল করা থাকলে Stack খালি থাকে, তাই Peek() এক্সেপশন থ্রো করবে।
  1. C# তে ContainsKey() পদ্ধতি

  2. সি শার্পে SequenceEqual মেথড

  3. C#-এ Aggregate মেথড

  4. C#-এ TakeWhile মেথড