C# Stack Peek
Description
Returns the object at the top of the Stack without removing it.
Syntax
public T Peek()
Returns
StackPeek
method returns The object at the top of the Stack.
Example
using System;//from ww w . j a v a2 s . c om
using System.Collections.Generic;
class Example
{
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");
Console.WriteLine(numbers.Peek());
}
}
The code above generates the following result.