本文整理汇总了C#中System.Collections.Queue.Dequeue方法的典型用法代码示例。如果您正苦于以下问题:C# Queue.Dequeue方法的具体用法?C# Queue.Dequeue怎么用?C# Queue.Dequeue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Queue
的用法示例。
在下文中一共展示了Queue.Dequeue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections;
public class SamplesQueue {
public static void Main() {
// Creates and initializes a new Queue.
Queue myQ = new Queue();
myQ.Enqueue( "The" );
myQ.Enqueue( "quick" );
myQ.Enqueue( "brown" );
myQ.Enqueue( "fox" );
// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );
// Removes an element from the Queue.
Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );
// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );
// Removes another element from the Queue.
Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );
// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );
// Views the first element in the Queue but does not remove it.
Console.WriteLine( "(Peek) \t{0}", myQ.Peek() );
// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );
}
public static void PrintValues( IEnumerable myCollection ) {
foreach ( Object obj in myCollection )
Console.Write( " {0}", obj );
Console.WriteLine();
}
}
输出:
Queue values: The quick brown fox (Dequeue) The Queue values: quick brown fox (Dequeue) quick Queue values: brown fox (Peek) brown Queue values: brown fox
示例2: Queue.Dequeue()
//引入命名空间
using System;
using System.Collections;
class MainClass {
public static void Main() {
Queue q = new Queue();
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(3);
q.Enqueue(4);
Console.Write("queue: ");
foreach(int i in q)
Console.Write(i + " ");
Console.WriteLine();
Console.Write("Dequeue -> ");
int a = (int) q.Dequeue();
Console.WriteLine(a);
Console.Write("queue: ");
foreach(int i in q)
Console.Write(i + " ");
Console.WriteLine();
}
}