Método Queue.Enqueue() en C#

Este método se utiliza para agregar un objeto al final de la cola. Esto viene bajo el espacio de nombres System.Collections. El valor puede anularse y si Count es menor que la capacidad de la array interna, este método es una operación O(1). Si es necesario reasignar la array interna para acomodar el nuevo elemento, este método se convierte en una operación O(n), donde n es Count.
Sintaxis: 
 

public virtual void Enqueue (object obj);

Aquí, obj es el objeto que se agrega a la cola.
Ejemplo:
 

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);
    }
}
Producción: 

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

 

Referencia:
 

Publicación traducida automáticamente

Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *