27 Mar

C# Queue Example

The Queue is a special collection which represents first-in, first-out concept of objects.

  • An element is added to the queue using the enqueue method. The statement’s general syntax is shown below:
queue.Enqueue("Obj");
  • To remove an element from the queue, the dequeue method is used. The dequeue operat on returns the queue’s last element. The statement’s general syntax is given below:
object obj = queue.Dequeue();

 

Queue Example:

Queue queue = new Queue();
queue.Enqueue("Obj1");
queue.Enqueue("Obj2");
queue.Enqueue("Obj3");

Console.WriteLine("The number of elements in the Queue: " + queue.Count);
Console.WriteLine("All objects");
foreach (Object obj in queue)
{
     Console.WriteLine(obj);
}

object obj1 = queue.Dequeue();
Console.WriteLine("First: " + obj1);

Console.WriteLine("Contains Obj1: " + queue.Contains("Obj1"));
Console.WriteLine("Contains Obj2: " + queue.Contains("Obj2"));

Program Output:
queue output

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir


üç − 3 =