本文整理匯總了C#中System.Collections.Queue.Peek方法的典型用法代碼示例。如果您正苦於以下問題:C# Queue.Peek方法的具體用法?C# Queue.Peek怎麽用?C# Queue.Peek使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Collections.Queue
的用法示例。
在下文中一共展示了Queue.Peek方法的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.Peek()
//引入命名空間
using System;
using System.Collections;
using System.Collections.Specialized;
class MyClass{
public string MyName="A";
}
class MainClass
{
static void Main(string[] args)
{
Queue classQueue = new Queue();
classQueue.Enqueue(new MyClass());
classQueue.Enqueue(new MyClass());
classQueue.Enqueue(new MyClass());
// Peek at first car in Q.
Console.WriteLine("First in Q is {0}",
((MyClass)classQueue.Peek()).MyName);
}
}