Share via


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:

See Also

Reference

Stack

System.Collections.Generic.Stack<T>

Queue

System.Collections.Generic.Queue<T>

ICollection

System.Collections.Generic.ICollection<T>

ConcurrentStack<T>

ConcurrentQueue<T>

Other Resources

Commonly Used Collection Types