Queue.Enqueue() Method in C#


The Queue.Enqueue() method in C# is used to add an object to the end of the Queue.

Syntax

The syntax is as follows −

public virtual void Enqueue (object ob);

Above, the parameter ob is the object to add to the Queue.

Example

Let us now see an example −

 Live Demo

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...");
      foreach(string i in queue){
         Console.WriteLine(i);
      }
      queue.Dequeue();
      queue.Dequeue();
      Console.WriteLine("Queue...UPDATED");
      foreach(string i in queue){
         Console.WriteLine(i);
      }
      Console.Write("Count of elements (updated) = "+queue.Count);
      queue.Clear();
      Console.Write("\nCount of elements (updated AGAIN) = ");
      Console.WriteLine(queue.Count);
   }
}

Output

This will produce the following output −

Count of elements = 8
Queue...
Gary
Jack
Ryan
Kevin
Mark
Jack
Ryan
Kevin
Queue...UPDATED
Ryan
Kevin
Mark
Jack
Ryan
Kevin
Count of elements (updated) = 6
Count of elements (updated AGAIN) = 0

Example

Let us now see another example −

 Live Demo

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");
      Console.WriteLine("Queue...");
      foreach(string i in queue){
         Console.WriteLine(i);
      }
      Console.Write("Count of elements = ");
      Console.WriteLine(queue.Count);
      queue.Enqueue("Jack");
      queue.Enqueue("Ryan");
      queue.Enqueue("Kevin");
      Console.WriteLine("\nQueue...UPDATED");
      foreach(string i in queue){
         Console.WriteLine(i);
      }
      Console.Write("Count of elements (Updated) = ");
      Console.WriteLine(queue.Count);
      queue.Dequeue();
      queue.Dequeue();
      Console.WriteLine("\nQueue...UPDATED");
      foreach(string i in queue){
         Console.WriteLine(i);
      }
      Console.Write("Count of elements = ");
      Console.WriteLine(queue.Count);
      queue.Clear();
      Console.Write("\nCount of elements (updated AGAIN) = ");
      Console.WriteLine(queue.Count);
   }
}

Output

This will produce the following output −

Queue...
Gary
Jack
Ryan
Kevin
Mark
Count of elements = 5
Queue...UPDATED
Gary
Jack
Ryan
Kevin
Mark
Jack
Ryan
Kevin
Count of elements (Updated) = 8
Queue...UPDATED
Ryan
Kevin
Mark
Jack
Ryan
Kevin
Count of elements = 6
Count of elements (updated AGAIN) = 0
raja
Published on 03-Dec-2019 07:18:16
Advertisements