# Queue<T> 類別

## 定義

``````generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollection``````
``````[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection``````
``````type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IEnumerable``````
``````Public Class Queue(Of T)
Implements ICollection, IEnumerable(Of T), IReadOnlyCollection(Of T)``````

T

Queue<T>

## 範例

ToArray 方法是用來建立陣列，並將佇列元素複製到其中，然後陣列會傳遞至接受 IEnumerable<T>Queue<T> 函數，建立佇列的複本。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.

Contains 方法是用來顯示在佇列的第一份複本中，字串 "四" 是在過去，Clear 方法會清除複本，而 Count 屬性會顯示佇列是空的。The Contains method is used to show that the string "four" is in the first copy of the queue, after which the Clear method clears the copy and the Count property shows that the queue is empty.

``````using System;
using System.Collections.Generic;

class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");

// A queue can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}

Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}",
numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());

// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());

Console.WriteLine("\nContents of the first copy:");
foreach( string number in queueCopy )
{
Console.WriteLine(number);
}

// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);

// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue<string> queueCopy2 = new Queue<string>(array2);

Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in queueCopy2 )
{
Console.WriteLine(number);
}

Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}",
queueCopy.Contains("four"));

Console.WriteLine("\nqueueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
}
}

/* This code example produces the following output:

one
two
three
four
five

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

Contents of the copy:
three
four
five

Contents of the second copy, with duplicates and nulls:

three
four
five

queueCopy.Contains("four") = True

queueCopy.Clear()

queueCopy.Count = 0
*/
``````
``````Imports System.Collections.Generic

Module Example

Sub Main

Dim numbers As New Queue(Of String)
numbers.Enqueue("one")
numbers.Enqueue("two")
numbers.Enqueue("three")
numbers.Enqueue("four")
numbers.Enqueue("five")

' A queue can be enumerated without disturbing its contents.
For Each number As String In numbers
Console.WriteLine(number)
Next

Console.WriteLine(vbLf & "Dequeuing '{0}'", numbers.Dequeue())
Console.WriteLine("Peek at next item to dequeue: {0}", _
numbers.Peek())
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue())

' Create a copy of the queue, using the ToArray method and the
' constructor that accepts an IEnumerable(Of T).
Dim queueCopy As New Queue(Of String)(numbers.ToArray())

Console.WriteLine(vbLf & "Contents of the first copy:")
For Each number As String In queueCopy
Console.WriteLine(number)
Next

' Create an array twice the size of the queue, compensating
' for the fact that Visual Basic allocates an extra array
' element. Copy the elements of the queue, starting at the
' middle of the array.
Dim array2((numbers.Count * 2) - 1) As String
numbers.CopyTo(array2, numbers.Count)

' Create a second queue, using the constructor that accepts an
' IEnumerable(Of T).
Dim queueCopy2 As New Queue(Of String)(array2)

Console.WriteLine(vbLf & _
"Contents of the second copy, with duplicates and nulls:")
For Each number As String In queueCopy2
Console.WriteLine(number)
Next

Console.WriteLine(vbLf & "queueCopy.Contains(""four"") = {0}", _
queueCopy.Contains("four"))

Console.WriteLine(vbLf & "queueCopy.Clear()")
queueCopy.Clear()
Console.WriteLine(vbLf & "queueCopy.Count = {0}", _
queueCopy.Count)
End Sub
End Module

' This code example produces the following output:
'
'one
'two
'three
'four
'five
'
'Dequeuing 'one'
'Peek at next item to dequeue: two
'
'Dequeuing 'two'
'
'Contents of the copy:
'three
'four
'five
'
'Contents of the second copy, with duplicates and nulls:
'
'
'
'three
'four
'five
'
'queueCopy.Contains("four") = True
'
'queueCopy.Clear()
'
'queueCopy.Count = 0
``````

## 備註

Queue<T> 和其元素上可以執行三個主要作業：Three main operations can be performed on a Queue<T> and its elements:

Queue<T> 的容量是 Queue<T> 可以保存的元素數目。The capacity of a Queue<T> is the number of elements the Queue<T> can hold. 當專案加入至 Queue<T>時，會依需要重新配置內部陣列，自動增加容量。As elements are added to a Queue<T>, the capacity is automatically increased as required by reallocating the internal array. 藉由呼叫 TrimExcess，可以降低容量。The capacity can be decreased by calling TrimExcess.

Queue<T> 接受 `null` 作為參考型別的有效值，並允許重複的元素。Queue<T> accepts `null` as a valid value for reference types and allows duplicate elements.

## 建構函式

 初始化 Queue 類別的新執行個體，這個執行個體為空白且具有預設的初始容量。Initializes a new instance of the Queue class that is empty and has the default initial capacity. 初始化 Queue 類別的新執行個體，其包含從指定之集合複製的項目，且具有容納複製之項目數目的足夠容量。Initializes a new instance of the Queue class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. 為具有指定初始容量且為空的 Queue 類別，初始化新的執行個體。Initializes a new instance of the Queue class that is empty and has the specified initial capacity.

## 屬性

 取得 Queue 中所包含的項目數。Gets the number of elements contained in the Queue.

## 方法

 從 Queue 移除所有物件。Removes all objects from the Queue. 判斷某項目是否在 Queue 中。Determines whether an element is in the Queue. 從指定的陣列索引處開始，複製 Queue 項目至現有一維 Array。Copies the Queue elements to an existing one-dimensional Array, starting at the specified array index. 移除並傳回在 Queue 開頭的物件。Removes and returns the object at the beginning of the Queue. 將物件加入至 Queue 的末端。Adds an object to the end of the Queue. 判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object. (繼承來源 Object) 傳回在 Queue 中逐一查看的列舉值。Returns an enumerator that iterates through the Queue. 做為預設雜湊函式。Serves as the default hash function. (繼承來源 Object) 取得目前執行個體的 Type。Gets the Type of the current instance. (繼承來源 Object) 建立目前 Object 的淺層複製。Creates a shallow copy of the current Object. (繼承來源 Object) 傳回 Queue 開頭的物件而不移除它。Returns the object at the beginning of the Queue without removing it. 將 Queue 項目複製到新的陣列。Copies the Queue elements to a new array. 傳回代表目前物件的字串。Returns a string that represents the current object. (繼承來源 Object) 如果該數目小於目前容量的 90%，則將容量設為 Queue 中項目的實際數目。Sets the capacity to the actual number of elements in the Queue, if that number is less than 90 percent of current capacity.

## 明確介面實作

 從特定的 ICollection 索引開始，將 Array 的項目複製到 Array。Copies the elements of the ICollection to an Array, starting at a particular Array index. 取得值，這個值表示對 ICollection 的存取是否同步 (安全執行緒)。Gets a value indicating whether access to the ICollection is synchronized (thread safe). 取得可用以同步存取 ICollection 的物件。Gets an object that can be used to synchronize access to the ICollection. 傳回逐一查看集合的列舉值。Returns an enumerator that iterates through a collection. 傳回逐一查看集合的列舉值。Returns an enumerator that iterates through a collection.

## 擴充方法

 根據輸入 DataTable 物件 (其中泛型參數 DataRow 為 IEnumerable) 傳回包含 `T` 物件複本的 DataRow。Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable object where the generic parameter `T` is DataRow. 根據輸入 DataRow 物件 (其中泛型參數 DataTable 為 IEnumerable)，將 `T` 物件複製到指定的 DataRow。Copies DataRow objects to the specified DataTable, given an input IEnumerable object where the generic parameter `T` is DataRow. 根據輸入 DataRow 物件 (其中泛型參數 DataTable 為 IEnumerable)，將 `T` 物件複製到指定的 DataRow。Copies DataRow objects to the specified DataTable, given an input IEnumerable object where the generic parameter `T` is DataRow. 將 IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type. 根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type. 啟用查詢的平行化作業。Enables parallelization of a query. Converts an IEnumerable to an IQueryable. 傳回包含來源集合中每個節點祖系的項目集合。Returns a collection of elements that contains the ancestors of every node in the source collection. 傳回包含來源集合中每個節點祖系的已篩選項目集合。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection. 傳回來源集合中每個文件和項目之子代節點的集合。Returns a collection of the descendant nodes of every document and element in the source collection. 傳回包含來源集合中每個項目和文件之子代項目的項目集合。Returns a collection of elements that contains the descendant elements of every element and document in the source collection. 傳回已篩選的項目集合，其中包含來源集合中每個項目和文件的子代項目。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection. 傳回來源集合中每個項目和文件的子項目集合。Returns a collection of the child elements of every element and document in the source collection. 傳回來源集合中每個項目和文件的已篩選子項目集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection. 傳回包含來源集合中所有節點的節點集合，依據文件順序來排序。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order. 傳回來源集合中每個文件和項目的子節點集合。Returns a collection of the child nodes of every document and element in the source collection. 在來源集合中，從每一個節點的父節點移除這些節點。Removes every node in the source collection from its parent node.