Queue 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示对象的先进先出集合。
public ref class Queue : System::Collections::ICollectionpublic ref class Queue : ICloneable, System::Collections::ICollectionpublic class Queue : System.Collections.ICollectionpublic class Queue : ICloneable, System.Collections.ICollection[System.Serializable]
public class Queue : ICloneable, System.Collections.ICollection[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Queue : ICloneable, System.Collections.ICollectiontype Queue = class
    interface ICollection
    interface IEnumerabletype Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable[<System.Serializable>]
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneablePublic Class Queue
Implements ICollectionPublic Class Queue
Implements ICloneable, ICollection- 继承
- 
				Queue
- 属性
- 实现
示例
以下示例演示如何创建 值并将其添加到 , Queue 以及如何输出其值。
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "Hello" );
   myQ->Enqueue( "World" );
   myQ->Enqueue( "!" );
   
   // Displays the properties and values of the Queue.
   Console::WriteLine( "myQ" );
   Console::WriteLine( "\tCount:    {0}", myQ->Count );
   Console::Write( "\tValues:" );
   PrintValues( myQ );
}
void PrintValues( IEnumerable^ myCollection )
{
   IEnumerator^ myEnum = myCollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", obj );
   }
   Console::WriteLine();
}
/* 
 This code produces the following output.
 
 myQ
     Count:    3
     Values:    Hello    World    !
*/
 using System;
 using System.Collections;
 public class SamplesQueue  {
    public static void Main()  {
       // Creates and initializes a new Queue.
       Queue myQ = new Queue();
       myQ.Enqueue("Hello");
       myQ.Enqueue("World");
       myQ.Enqueue("!");
       // Displays the properties and values of the Queue.
       Console.WriteLine( "myQ" );
       Console.WriteLine( "\tCount:    {0}", myQ.Count );
       Console.Write( "\tValues:" );
       PrintValues( myQ );
    }
    public static void PrintValues( IEnumerable myCollection )  {
       foreach ( Object obj in myCollection )
          Console.Write( "    {0}", obj );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.
 myQ
     Count:    3
     Values:    Hello    World    !
*/
Imports System.Collections
Public Class SamplesQueue
    Public Shared Sub Main()
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("Hello")
        myQ.Enqueue("World")
        myQ.Enqueue("!")
        ' Displays the properties and values of the Queue.
        Console.WriteLine("myQ")
        Console.WriteLine("    Count:    {0}", myQ.Count)
        Console.Write("    Values:")
        PrintValues(myQ)
    End Sub
    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub
End Class
' This code produces the following output.
' 
' myQ
'     Count:    3
'     Values:    Hello    World    !
注解
此类将队列实现为循环数组。 存储在 中的 Queue 对象插入到一端,并从另一端删除。
需要临时存储信息时,队列和堆栈非常有用;也就是说,你可能希望在检索元素值后放弃元素。 如果需要按照信息在集合中的存储顺序访问信息,请使用 Queue 。 如果需要按相反顺序访问信息,请使用 Stack 。 如果需要同时从多个线程访问集合,请使用 ConcurrentQueue<T> 或 ConcurrentStack<T> 。
可以对 及其元素执行三个Queuemain操作:
的 Queue 容量是 可以容纳的元素 Queue 数。 将元素添加到 时 Queue,容量会根据需要通过重新分配自动增加。 可以通过调用 TrimToSize来减小容量。
增长因子是当需要更大的容量时,当前容量乘以的数字。 增长因子是在构造 时 Queue 确定的。 默认增长因子为 2.0。 的容量 Queue 将始终至少增加 4,而不管增长因素如何。 例如,当需要更大的容量时, Queue 增长因子为 1.0 的 将始终将容量增加 4。
              Queue 接受 null 作为有效值,并允许重复元素。
有关此集合的泛型版本,请参阅 System.Collections.Generic.Queue<T>
构造函数
| Queue() | 初始化 Queue 类的新实例,该实例为空,具有默认初始容量并使用默认增长因子。 | 
| Queue(ICollection) | 初始化 Queue 类的新实例,该实例包含从指定集合复制的元素,具有与所复制的元素数相同的初始容量并使用默认增长因子。 | 
| Queue(Int32) | 初始化 Queue 类的新实例,该实例为空,具有指定的初始容量并使用默认增长因子。 | 
| Queue(Int32, Single) | 初始化 Queue 类的新实例,该实例为空,具有指定的初始容量并使用指定的增长因子。 | 
属性
| Count | 获取 Queue 中包含的元素数。 | 
| IsSynchronized | 获取一个值,该值指示是否同步对 Queue 的访问(线程安全)。 | 
| SyncRoot | 获取可用于同步对 Queue 的访问的对象。 | 
方法
| Clear() | 从 Queue 中移除所有对象。 | 
| Clone() | 创建 Queue 的浅表副本。 | 
| Contains(Object) | 确定某元素是否在 Queue 中。 | 
| CopyTo(Array, Int32) | |
| Dequeue() | 移除并返回位于 Queue 开始处的对象。 | 
| Enqueue(Object) | 将对象添加到 Queue 的结尾处。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetEnumerator() | 返回循环访问 Queue 的枚举数。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Peek() | 返回位于 Queue 开始处的对象但不将其移除。 | 
| Synchronized(Queue) | 返回将包装原始队列并且是线程安全的新的 Queue。 | 
| ToArray() | 将 Queue 元素复制到新数组。 | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
| TrimToSize() | 将容量设置为 Queue 中元素的实际数目。 | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 | 
适用于
线程安全性
Visual Basic 中的公共静态 (Shared) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。
若要保证 的 Queue线程安全,所有操作都必须通过 方法返回的 Synchronized(Queue) 包装器完成。
枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。