Mid Collections

How do you access the top element in a Stack<T> without removing it?

Use the Peek() method.

Example:

int top = stack.Peek();

Follow:

This is useful when you just want to inspect the top element without altering the stack.

More from C# Programming Tutorial

All questions for this course