Export (0) Print
Expand All

Queue<T>.Enqueue Method

Adds an object to the end of the Queue<T>.

Namespace:  System.Collections.Generic
Assemblies:   System (in System.dll)
  System.Collections (in System.Collections.dll)

public void Enqueue(
	T item


Type: T

The object to add to the Queue<T>. The value can be null for reference types.

If Count already equals the capacity, the capacity of the Queue<T> is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.


The following code example demonstrates several methods of the Queue<T> generic class, including the Enqueue method.


The ToArray method is used to create an array and copy the queue elements to it, then the array is passed to the Queue<T> constructor that takes IEnumerable<T>, creating a copy of the queue. The elements of the copy are displayed.

An array twice the size of the queue is created, and the CopyTo method is used to copy the array elements beginning at the middle of the array. The Queue<T> constructor is used again to create a second copy of the queue containing three null elements at the beginning.


using System;
using System.Collections.Generic;

class Example
    public static void Main()

        // A queue can be enumerated without disturbing its contents. 
        foreach( string number in numbers )


        // constructor that accepts an IEnumerable<T>.

        foreach( string number in queueCopy )

        // array.  
        numbers.CopyTo(array2, numbers.Count);

        // IEnumerable(Of T).

        foreach( string number in queueCopy2 )



/* This code example produces the following output:


Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'

Contents of the copy:

Contents of the second copy, with duplicates and nulls:


queueCopy.Contains("four") = True


queueCopy.Count = 0

.NET Framework


.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8



© 2014 Microsoft