本文整理汇总了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);
}
}