English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Stack is a special collection type that stores elements in Last In, First Out (LIFO) order. C# includes generic Stack<T> and non-generic Stack collection class. It is recommended to use the generic Stack<T> collection.
The stack is very useful for storing temporary data in LIFO form, and you may want to delete the element after retrieving its value.
Stack<T> is a Last In, First Out (LIFO) collection.
It is under the System.Collection.Generic namespace.
Stack<T> can contain elements of a specified type. It provides compile-time type checking and does not perform boxing/unboxing, because it is generic.
You can use the Push() method to add elements. You cannot use collection initialization because it is generic.-initializer syntax.
You can retrieve elements using the Pop() and Peek() methods. It does not support indexers.
You can create an object by specifying the type parameter for the element type stored in Stack<T>. The following example uses the Push() method to create and add elements to Stack <T>. The stack allows null (for reference types) and duplicate values.
Stack<int> myStack = new Stack<int>(); myStack.Push(1); myStack.Push(2); myStack.Push(3); myStack.Push(4); foreach (var item in myStack) Console.Write(item + "," //Print4,3,2,1,
You can also create a stack from an array as follows.
int[] arr = new int[]{ 1, 2, 3, 4}; Stack<int> myStack = new Stack<int>(arr); foreach (var item in myStack) Console.Write(item + "," //Print4,3,2,1,
Property | Usage |
---|---|
Count | Return the total number of elements in the stack. |
Method | Usage |
---|---|
Push(T) | Insert an item at the top of the stack. |
Peek() | Return the item at the top of the stack. |
Pop() | Remove and return the item at the top of the stack. |
Contains(T) | Check if there is an item in the stack. |
Clear() | Remove all items from the stack. |
The Pop() method returns the last element and removes it from the stack. If the stack is empty, it will throw an InvalidOperationException. Therefore, always check the number of elements in the stack before calling the Pop() method.
Stack<int> myStack = new Stack<int>(); myStack.Push(1); myStack.Push(2); myStack.Push(3); myStack.Push(4); Console.Write("Number of elements in the stack: {0}", myStack.Count); while (myStack.Count > 0) Console.Write(myStack.Pop() + "," Console.Write("Number of elements in the stack: {0}", myStack.Count);
Number of elements in the stack:4 4,3,2,1, Number of elements in the stack: 0
The Peek() method returns the last added value from the stack but does not remove it. Calling the Peek() method on an empty stack will throw an InvalidOperationException. Therefore, always check the number of elements in the stack before retrieving an element using the Peek() method.
Stack<int> myStack = new Stack<int>(); myStack.Push(1); myStack.Push(2); myStack.Push(3); myStack.Push(4); Console.Write("Number of elements in Stack: {0}", myStack.Count);// Output 4 if(myStack.Count > 0){ Console.WriteLine(myStack.Peek()); // Output 4 Console.WriteLine(myStack.Peek()); // Output 4 } Console.Write("Number of elements in Stack: {0}", myStack.Count);// Output 4
The Contains() method checks if the specified element exists in the Stack collection. If it exists, it returns true; otherwise, it returns false.
Stack<int> myStack = new Stack<int>(); myStack.Push(1); myStack.Push(2); myStack.Push(3); myStack.Push(4); myStack.Contains(2); // Return true myStack.Contains(10); // Return false