At its most basic level, a stack is a collection of elements of the same specified type. The stack length is variable, meaning it can change depending on how many elements it's holding. The important difference between a stack and a list or array is how the elements are stored. Stacks follow the last-in-first-out (LIFO) model, meaning the last element in the stack is the first accessible element. This is useful when you want to access elements in reverse order. You should note that they can store null and duplicate values.
All the collection types in this chapter are a part of the System.Collections.Generic namespace, meaning you need to add the following code to the top of any file that you want to use them in:
using System.Collections.Generic;
using System.Collections.Generic;
Now that you know what you're about to work with, let's take a look at the basic syntax for declaring stacks.