C#-এ Queue.Peek() পদ্ধতিটি সারির শুরুতে বস্তুটিকে অপসারণ না করে ফেরত দিতে ব্যবহৃত হয়।
সিনট্যাক্স
সিনট্যাক্স নিম্নরূপ -
public virtual object Peek ();
উদাহরণ
আসুন এখন একটি উদাহরণ দেখি -
using System; using System.Collections; public class Demo { public static void Main() { Queue queue = new Queue(); queue.Enqueue("AB"); queue.Enqueue("BC"); queue.Enqueue("CD"); queue.Enqueue("DE"); queue.Enqueue("EF"); queue.Enqueue("FG"); queue.Enqueue("GH"); queue.Enqueue("HI"); Console.WriteLine("Queue..."); IEnumerator demoEnum = queue.GetEnumerator(); while (demoEnum.MoveNext()) { Console.WriteLine(demoEnum.Current); } Console.WriteLine("Queue element at the beginning = "+queue.Peek()); Console.WriteLine("Is Queue synchronized? = "+queue.IsSynchronized); Queue queue2 = Queue.Synchronized(queue); Console.WriteLine("Is Queue synchronized now? = "+queue2.IsSynchronized); } }
আউটপুট
এটি নিম্নলিখিত আউটপুট −
তৈরি করবেQueue... AB BC CD DE EF FG GH HI Queue element at the beginning = AB Is Queue synchronized? = False Is Queue synchronized now? = True
উদাহরণ
এখন আরেকটি উদাহরণ দেখা যাক -
using System; using System.Collections.Generic; public class Demo { public static void Main() { Queue<string> queue = new Queue<string>(); queue.Enqueue("Gary"); queue.Enqueue("Jack"); queue.Enqueue("Ryan"); queue.Enqueue("Kevin"); queue.Enqueue("Mark"); queue.Enqueue("Jack"); queue.Enqueue("Ryan"); queue.Enqueue("Kevin"); Console.Write("Count of elements = "); Console.WriteLine(queue.Count); Console.WriteLine("Queue element at the beginning = "+queue.Peek()); Console.WriteLine("Does the queue has element Jack? = "+queue.Contains("Jack")); queue.Clear(); Console.Write("Count of elements (updated) = "); Console.WriteLine(queue.Count); } }
আউটপুট
এটি নিম্নলিখিত আউটপুট −
তৈরি করবেCount of elements = 8 Queue element at the beginning = Gary Does the queue has element Jack? = True Count of elements (updated) = 0