เมธอด Stack.Peek() ใน C# ใช้สำหรับดูค่าที่อยู่ด้านบน (top) ของ Stack โดยไม่ลบออก — ต่างจาก Pop() ที่ลบและคืนค่า
ไวยากรณ์ (Syntax)
public virtual object Peek ();
ตัวอย่างที่ 1: Peek กับ Stack ของสตริง
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
ตัวอย่างที่ 2: Peek พร้อม Clone และ Contains
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 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("Element Alienware is in 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 Earphone Keyboard Headphone Notebook Laptop XPS Monitors Projectors Alienware Inspiron Element at the top = Earphone Count of elements (updated) = 10 Count of elements (updated) = 0
ข้อสังเกตสำคัญ
Peek()คืนค่าองค์ประกอบด้านบน โดยไม่ลบออก — Stack คงเดิม- หาก Stack ว่าง (
Count == 0) การเรียก Peek() จะโยนInvalidOperationException - ควรตรวจสอบ
Count > 0ก่อนเรียก Peek() เสมอ - Stack เป็น LIFO (Last In, First Out) — Push ล่าสุด = Top
- เวอร์ชัน Generic
Stack<T>.Peek()คืนค่าแบบ T โดยตรง ไม่ต้องแคสต์