Skip to content
Related Articles

Related Articles

Improve Article

Queue.Enqueue() Method in C#

  • Last Updated : 13 Apr, 2021

This method is used to add an object to the end of the Queue. This comes under the System.Collections namespace. The value can null and if the Count is less than the capacity of the internal array, this method is an O(1) operation. If the internal array needs to be reallocated to accommodate the new element, this method becomes an O(n) operation, where n is Count.
Syntax : 
 

public virtual void Enqueue (object obj);

Here, obj is the object which is added to the Queue.
Example:
 

CSHARP




// C# code to illustrate the
// Queue.Enqueue() Method
using System;
using System.Collections;
 
class GFG {
 
    // Driver code
    public static void Main()
    {
 
        // Creating a Queue
        Queue myQueue = new Queue();
 
        // Inserting the elements into the Queue
        myQueue.Enqueue("one");
 
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
 
        Console.WriteLine(myQueue.Count);
 
        myQueue.Enqueue("two");
 
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
 
        Console.WriteLine(myQueue.Count);
 
        myQueue.Enqueue("three");
 
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
 
        Console.WriteLine(myQueue.Count);
 
        myQueue.Enqueue("four");
 
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
 
        Console.WriteLine(myQueue.Count);
 
        myQueue.Enqueue("five");
 
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
 
        Console.WriteLine(myQueue.Count);
 
        myQueue.Enqueue("six");
 
        // Displaying the count of elements
        // contained in the Queue
        Console.Write("Total number of elements in the Queue are : ");
 
        Console.WriteLine(myQueue.Count);
    }
}
Output: 
Total number of elements in the Queue are : 1
Total number of elements in the Queue are : 2
Total number of elements in the Queue are : 3
Total number of elements in the Queue are : 4
Total number of elements in the Queue are : 5
Total number of elements in the Queue are : 6

 

Reference:
 

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :