Stack Collection Types
The System.Collections.Stack class, and the System.Collections.Generic.Stack<T> and System.Collections.Concurrent.ConcurrentStack<T> generic classes are last-in, first-out collection classes that implement the ICollection interface. The System.Collections.Generic.Stack<T> and System.Collections.Concurrent.ConcurrentStack<T> generic classes also implement the ICollection<T> generic interface.
Stacks and queues are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use System.Collections.Queue if you need to access the information in the same order that it is stored in the collection. Use System.Collections.Generic.Stack<T> if you need to access the information in reverse order.
Use the System.Collections.Concurrent.ConcurrentStack<T> and System.Collections.Concurrent.ConcurrentQueue<T> types when you need to access the collection from multiple threads concurrently.
A common use for System.Collections.Generic.Stack<T> is to preserve variable states during calls to other procedures.
Three main operations can be performed on a System.Collections.Generic.Stack<T> and its elements:
Peek returns an element that is at the top of the Stack<T> but does not remove it from the Stack<T>.
The System.Collections.Concurrent.ConcurrentStack<T> class provides TryPop and TryPopRange methods that return false (False in Visual Basic) if the value or values could not be popped. The TryPopRange and PushRange methods provide efficient pushing and popping of multiple elements in a single operation.
See Also
Reference
System.Collections.Generic.Stack<T>
System.Collections.Generic.Queue<T>
System.Collections.Generic.ICollection<T>