Latest web development tutorials

C # queue (Queue)

C # set C # set

Queue (Queue) represents afirst-object collection.When you need access to the FIFO, use the queue. When you add a list, calledinto the team, when you remove the item from the list, called a team.

Queue methods and properties of the class

The following table lists some of the commonattributes Queueclass:

属性描述
Count获取 Queue 中包含的元素个数。

The following table lists some of the commonmethods Queueclass:

序号方法名 & 描述
1public virtual void Clear();
从 Queue 中移除所有的元素。
2public virtual bool Contains( object obj );
判断某个元素是否在 Queue 中。
3public virtual object Dequeue();
移除并返回在 Queue 的开头的对象。
4public virtual void Enqueue( object obj );
向 Queue 的末尾添加一个对象。
5public virtual object[] ToArray();
复制 Queue 到一个新的数组中。
6public virtual void TrimToSize();
设置容量为 Queue 中元素的实际个数。

Examples

The following example demonstrates the use of the queue (Queue) is:

using System;
using System.Collections;

namespace CollectionsApplication
{
   class Program
   {
      static void Main (string [] args)
      {
         Queue q = new Queue ();

         q.Enqueue ( 'A');
         q.Enqueue ( 'M');
         q.Enqueue ( 'G');
         q.Enqueue ( 'W');
         
         Console.WriteLine ( "Current queue:");
         foreach (char c in q)
            Console.Write (c + "");
         Console.WriteLine ();
         q.Enqueue ( 'V');
         q.Enqueue ( 'H');
         Console.WriteLine ( "Current queue:");         
         foreach (char c in q)
            Console.Write (c + "");
         Console.WriteLine ();
         Console.WriteLine ( "Removing some values");
         char ch = (char) q.Dequeue ();
         Console.WriteLine ( "The removed value: {0}", ch);
         ch = (char) q.Dequeue ();
         Console.WriteLine ( "The removed value: {0}", ch);
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Current queue: 
AMGW 
Current queue: 
AMGWVH 
Removing values
The removed value: A
The removed value: M

C # set C # set