生命之价
---程序员
命名空间:  System.Collections.Generic
程序集:  System(在 System.dll 中)
T

指定队列中元素的类型。

队列在按接收顺序存储消息方面非常有用,以便于进行顺序处理。存储在 Queue<(Of <(T>)>) 中的对象在一端插入,从另一端移除。

Queue<(Of <(T>)>) 的容量是指 Queue<(Of <(T>)>) 可以容纳的元素数。当向 Queue<(Of <(T>)>) 添加元素时,将通过重新分配内部数组,根据需要自动增大容量。可通过调用 TrimExcess 来减少容量。

Queue<(Of <(T>)>) 接受 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing 作为引用类型的有效值并且允许有重复的元素。

下面的代码示例演示了 Queue<(Of <(T>)>) 泛型类的几个方法。此代码示例创建一个具有默认容量的字符串队列,并使用 Enqueue 方法对五个字符串进行排队。枚举队列元素,这不会更改队列的状态。使用 Dequeue 方法使第一个字符串出列。使用 Peek 方法查找队列中的下一项,然后使用 Dequeue 方法使该项出列。

使用 ToArray 方法创建一个数组并将队列元素复制到该数组,然后将该数组传递给接受 IEnumerable<(Of <(T>)>)Queue<(Of <(T>)>) 构造函数以创建队列副本。将显示副本的元素。

创建一个大小是队列大小两倍的数组,并使用 CopyTo 方法从数组中间开始复制数组元素。再次使用 Queue<(Of <(T>)>) 构造函数创建第二个队列副本,此队列在开始处包含三个 null 元素。

使用 Contains 方法显示字符串“four”在第一个队列副本中,然后使用 Clear 方法清除此副本并由 Count 属性显示该队列为空。

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
*/


System..::.Object
  System.Collections.Generic..::.Queue<(Of <(T>)>)

此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。

只要不修改该集合,Queue<(Of <(T>)>) 就可以同时支持多个阅读器。即便如此,从头到尾对一个集合进行枚举本质上并不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

 

文章摘自

posted on 2008-11-21 13:53  Freeman Shen  阅读(707)  评论(0)    收藏  举报